2-sat->poj 3648 Wedding
2013-10-12 09:52
387 查看
Wedding
DescriptionUp 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 sameside 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 itis 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.InputThe 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 fromcouple 2), or "10w 4w", or "3h 1h". Couples are numbered from 0 to n - 1 with the bride and groom being 0w and 0h.OutputFor 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
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 7406 | Accepted: 2254 | Special Judge |
10 6 3h 7h 5w 3w 7h 6w 8w 3w 7h 3w 2w 5h 0 0Sample Output
1h 2h 3w 4h 5h 6h 7h 8h 9h
构图求解:
新娘编号0, 从n - 1 对夫妇中选择n - 1个不相容的坐在新娘的对面。加边add(0, n),新娘必不选。
#include <iostream>#include <algorithm>#include <string>#include <string.h>#include <stack>#include <queue>using namespace std;#define MAXN 200 + 10struct Edge{int u, v, next;}edge[200 * MAXN], edgetwo[200 * MAXN];int head[MAXN], e, etwo, cnt, ind, n, m;int dfn[MAXN], low[MAXN], belong[MAXN], cf[MAXN], col[MAXN];int indegree[MAXN], headtwo[MAXN];bool ins[MAXN];void add(int u, int v){edge[e].u = u;edge[e].v = v;edge[e].next = head[u];head[u] = e++;}void insert(int u, int v){edgetwo[etwo].v = v;edgetwo[etwo].next = headtwo[u];headtwo[u] = etwo++;}queue <int> q;stack <int> s;void init(){e = etwo = ind = cnt = 0;while (!q.empty()){q.pop();}while (!s.empty()){s.pop();}memset(head, -1, sizeof(head));memset(headtwo, -1, sizeof(headtwo));memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(indegree, 0, sizeof(indegree));memset(cf, 0, sizeof(cf));memset(col, 0, sizeof(col));memset(belong, 0, sizeof(belong));memset(ins, false, sizeof(ins));}void tarjan(int u){dfn[u] = low[u] = ++ind;ins[u] = true;s.push(u);for (int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;if (!dfn[v]){tarjan(v);low[u] = min(low[u], low[v]);}else if (ins[v]){low[u] = min(low[u], dfn[v]);}}if (low[u] == dfn[u]){cnt++;int x;do{x = s.top();s.pop();ins[x] = false;belong[x] = cnt;}while (x != u);}}void torpu(){for (int i = 1; i <= cnt; i++){if (indegree[i] == 0){q.push(i);}}while (!q.empty()){int cur = q.front();q.pop();if (col[cur] == 0){col[cur] = 1;col[cf[cur]] = -1;}for (int i = headtwo[cur]; i != -1; i = edgetwo[i].next){int v = edgetwo[i].v;if (--indegree[v] == 0){q.push(v);}}}}void solve(){for (int i = 0; i < 2 * n; i++){if (!dfn[i]){tarjan(i);}}bool flag = true;for (int i = 0; i < n; i++){if (belong[i] == belong[i + n]){flag = false;}cf[belong[i]] = belong[i + n];cf[belong[i + n]] = belong[i];}if (!flag){cout << "bad luck" << endl;}else{for (int i = 0; i < e; i++){if (belong[edge[i].u] != belong[edge[i].v]){insert(belong[edge[i].v], belong[edge[i].u]);indegree[belong[edge[i].u]]++;}}torpu();int num = 0;for (int i = 1; i < n; i++){if (col[belong[i]] == 1){cout << i << 'h';}else{cout << i << 'w';}if (i != n - 1){cout << ' ';}}cout << endl;}}void input(){int u, v;char ch1, ch2;while (cin >> n >> m, n + m){init();for (int i = 0; i < m; i++){cin >> u >> ch1 >> v >> ch2;if (v == 0 && ch2 == 'w'){continue;}if (ch1 == 'w' && ch2 == 'w'){add(u, v + n);add(v, u + n);}else if (ch1 == 'w' && ch2 == 'h'){add(u, v);add(v + n, u + n);}else if (ch1 == 'h' && ch2 == 'w'){add(v, u);add(u + n, v + n);}else{add(u + n, v);add(v + n, u);}}add(0, 0 + n);solve();}}int main(){input();return 0;}[/code]
相关文章推荐
- poj 3648 Wedding(2-SAT)
- poj 3648 Wedding(2-sat 求解)
- 2-sat->poj 3207 Ikki's Story IV - Panda's Trick
- poj 3648 Wedding 2-sat
- Wedding (poj 3648 2-SAT 输出任意一组解)
- POJ 3648 Wedding 2-sat输出一组解
- 2-sat->poj 2723 Get Luffy Out
- POJ 3648 Wedding (2-SAT+输出可行解)
- POJ 3648-Wedding(2-SAT)
- |poj 3648|2-SAT|Wedding
- POJ 3648-Wedding(2-SAT)
- 2-SAT ( Tarjan )——Wedding (POJ 3648)
- poj 3648 Wedding(2-sat--拓扑排序输出可行解)
- POJ 3648 Wedding 2-sat
- POJ 3648 Wedding(2-SAT)
- poj 3648 Wedding 2sat
- POJ 3648 Wedding 2-SAT
- POJ 3648 - Wedding...阅读理解.题意坑爹.2-sat..细节注意
- POJ 3648 Wedding (2-SAT,经典)
- 2-sat&&http://poj.org/problem?id=3648