码迷,mamicode.com
首页 > 其他好文 > 详细

uva 11294

时间:2014-05-05 22:10:59      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   tar   color   

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
bubuko.com,布布扣
  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\n");
 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 ? \n :   );
 98                     }
 99             }
100     }
101     //cout << "Hello world!" << endl;
102     return 0;
103 }
View Code

 

uva 11294,布布扣,bubuko.com

uva 11294

标签:style   blog   class   code   tar   color   

原文地址:http://www.cnblogs.com/hyxsolitude/p/3704920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!