zoukankan      html  css  js  c++  java
  • uva 11294

    Problem E: Wedding

    Up to thirty couples will attend a wedding feast, at which they will be seated on either side of a long table. The bride and groom sit at one end, opposite each other, and the bride wears an elaborate headdress that keeps her from seeing people on the same side as her. It is considered bad luck to have a husband and wife seated on the same side of the table. Additionally, there are several pairs of people conducting adulterous relationships (both different-sex and same-sex relationships are possible), and it is bad luck for the bride to see both members of such a pair. Your job is to arrange people at the table so as to avoid any bad luck.

    The input consists of a number of test cases, followed by a line containing 0 0. Each test case gives n, the number of couples, followed by the number of adulterous pairs, followed by the pairs, in the form "4h 2w" (husband from couple 4, wife from couple 2), or "10w 4w", or "3h 1h". Couples are numbered from 0 to n-1 with the bride and groom being 0w and 0h. For each case, output a single line containing a list of the people that should be seated on the same side as the bride. If there are several solutions, any one will do. If there is no solution, output a line containing "bad luck".

    Sample Input

    10 6
    3h 7h
    5w 3w
    7h 6w
    8w 3w
    7h 3w
    2w 5h
    0 0
    

    Possible Output for Sample Input

    1h 2h 3w 4h 5h 6h 7h 8h 9h

    2-sat
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <stack>
      6 #include <vector>
      7 
      8 using namespace std;
      9 
     10 const int MAX_N = 50;
     11 int N,M;
     12 int low[4 * MAX_N],pre[4 * MAX_N],cmp[4 * MAX_N];
     13 vector<int> G[4 * MAX_N];
     14 stack<int> S;
     15 int dfs_clock,scc_cnt;
     16 int ans[MAX_N * 2];
     17 
     18 void dfs(int u) {
     19         low[u] = pre[u] = ++dfs_clock;
     20         S.push(u);
     21         for(int i = 0; i < G[u].size(); ++i) {
     22                 int v = G[u][i];
     23                 if(!pre[v]) {
     24                         dfs(v);
     25                         low[u] = min(low[u],low[v]);
     26                 } else if(!cmp[v]) {
     27                         low[u] = min(low[u],pre[v]);
     28                 }
     29         }
     30 
     31         if(pre[u] == low[u]) {
     32                 ++scc_cnt;
     33                 for(;;) {
     34                         int x = S.top(); S.pop();
     35                         cmp[x] = scc_cnt;
     36                         if(x == u) break;
     37                 }
     38         }
     39 }
     40 
     41 bool scc() {
     42         dfs_clock = scc_cnt = 0;
     43         memset(cmp,0,sizeof(cmp));
     44         memset(pre,0,sizeof(pre));
     45 
     46         for(int i = 0; i < 4 * N; ++i) if(!pre[i]) dfs(i);
     47 
     48         for(int i = 0; i < 2 * N; ++i) {
     49                 if(cmp[i] == cmp[2 * N + i]) return false;
     50         }
     51 
     52         return true;
     53 }
     54 
     55 int main()
     56 {
     57    // freopen("sw.in","r",stdin);
     58     while(~scanf("%d%d",&N,&M) && (N || M)) {
     59             for(int i = 0; i < 4 * N; ++i) G[i].clear();
     60             for(int i = 0; i < N; ++i) {
     61                     G[i].push_back(i + N + 2 * N);
     62                     G[i + 2 * N].push_back(i + N);
     63                     G[i + N].push_back(i + 2 * N);
     64                     G[i + 3 * N].push_back(i);
     65             }
     66             G[2 * N + N].push_back(N);
     67             G[0].push_back(2 * N);
     68 
     69 
     70             for(int i = 1; i <= M; ++i) {
     71                     int u,v;
     72                     char ch1,ch2;
     73                     scanf("%d%c%d%c",&u,&ch1,&v,&ch2);
     74                     u += ch1 == 'h' ? N : 0;
     75                     v += ch2 == 'h' ? N : 0;
     76                     G[u].push_back(v + 2 * N);
     77                     G[v].push_back(u + 2 * N);
     78 
     79             }
     80 
     81             if(!scc()) {
     82                     printf("bad luck
    ");
     83             } else {
     84                     int len = 0;
     85                     for(int i = 1; i < N; ++i) {
     86                             if(cmp[i + 2 * N] < cmp[i]) {
     87                                     ans[len++] = i;
     88                             }
     89                             if(cmp[i + N + 2 * N] < cmp[i + N]) {
     90                                     ans[len++] = i + N;
     91                             }
     92                     }
     93 
     94                     for(int i = 0; i < len; ++i) {
     95                             printf("%d%c",ans[i] >= N ? ans[i] - N : ans[i],
     96                                           ans[i] >= N ? 'h' : 'w');
     97                             printf("%c",i == len - 1 ? '
    ' : ' ' );
     98                     }
     99             }
    100     }
    101     //cout << "Hello world!" << endl;
    102     return 0;
    103 }
    View Code
  • 相关阅读:
    Python 连接SQLite数据库 及基础操作
    删除爬取字符串中的特殊字符
    网页爬虫中xa0、u3000等字符的解释及去除
    File 文件操作及模式说明
    【re】模块运用,正则匹配操作 待编辑
    MySQL
    正则表达式的常用操作符
    pip操作
    Python 常见运算
    Python32 1.半连接数 2.粘包问题解决
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3704920.html
Copyright © 2011-2022 走看看