poj 3648 Wedding 2-SAT问题入门题目

Description

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.

Input

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.

Output

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

Sample Output

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

这题题目题意简直可怕
有一对新人结婚,邀请n对夫妇去参加婚礼。
有一张很长的桌子,人只能坐在桌子的两边,还要满
足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇
之中可能有通奸关系(包括男男,男女,女女),有通
奸关系的不能同时坐在新娘的对面,可以分开坐,可以
同时坐在新娘这一侧。如果存在一种可行的方案,输出
与新娘同侧的人。


 

源地址 https://blog.csdn.net/u012915516/article/details/48442265

SAT这种问题 和网络流一样 只要图建对了 就对了
主要是建图问题

   特殊情况:当新郞有奸情的时候,与他有奸情的必需选择了(新浪在对面),

    当新娘有奸情时候没关系,不处理。



 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <string>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <stack>
 9 
10 using namespace std;
11 const int maxn = 1e5 + 10;
12 const int  mod = 1e9 + 7 ;
13 const int INF = 0x7ffffff;
14 struct node {
15     int v, next;
16 } edge[maxn];
17 int head[maxn], dfn[maxn], low[maxn];
18 int s[maxn], belong[maxn], instack[maxn];
19 int tot, cnt, top, flag, n, m;
20 void init() {
21     tot = cnt = top = flag = 0;
22     memset(s, 0, sizeof(s));
23     memset(head, -1, sizeof(head));
24     memset(dfn, 0, sizeof(dfn));
25     memset(instack, 0, sizeof(instack));
26 }
27 void add(int u, int v ) {
28     edge[tot].v = v;
29     edge[tot].next = head[u];
30     head[u] = tot++;
31 }
32 void tarjan(int v) {
33     dfn[v] = low[v] = ++flag;
34     instack[v] = 1;
35     s[top++] = v;
36     for (int i = head[v] ; ~i ; i = edge[i].next ) {
37         int j = edge[i].v;
38         if (!dfn[j]) {
39             tarjan(j);
40             low[v] = min(low[v], low[j]);
41         } else if (instack[j]) low[v] = min(low[v], dfn[j]);
42     }
43     if (dfn[v] == low[v]) {
44         cnt++;
45         int t;
46         do {
47             t = s[--top];
48             instack[t] = 0;
49             belong[t] = cnt;
50         } while(t != v) ;
51     }
52 }
53 int ans[maxn];
54 int check() {
55     for (int i = 0 ; i < 2 * n ; i++)
56         if (!dfn[i]) tarjan(i);
57     for (int i = 0 ; i < 2 * n ; i += 2) {
58         if (belong[i] == belong[i + 1]) return 0;
59         if (belong[i] < belong[i + 1]) ans[i / 2] = i;
60         else ans[i / 2] = i + 1;
61     }
62     return 1;
63 }
64 int main() {
65     while(scanf("%d%d", &n, &m) != EOF) {
66         if (n == 0 && m == 0) break;
67         int t1, t2;
68         char c1, c2;
69         init();
70         memset(ans, 0, sizeof(ans));
71         for (int i = 0 ; i < m ; i++) {
72             scanf("%d%c%d%c", &t1, &c1, &t2, &c2);
73             if (c1 == 'h') t1 = t1 * 2 + 1;
74             else t1 = t1 * 2;
75             if (c2 == 'h') t2 = t2 * 2 + 1;
76             else t2 = t2 * 2;
77             if (t1 == 1) add(t2 ^ 1, t2);
78             else if (t2 == 1) add(t1 ^ 1, t1);
79             else if (t1 == 0 || t1 == 0) {}
80             else {
81                 add(t1 ^ 1, t2);
82                 add(t2 ^ 1, t1);
83             }
84         }
85         if (check()) {
86             for (int i = 1 ; i < n ; i++) {
87                 if (ans[i] == i * 2) printf("%dw ", i);
88                 else printf("%dh ", i);
89             }
90             printf("\n");
91         } else printf("bad luck\n");
92 
93     }
94     return 0;
95 }

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9094461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值