Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10975   Accepted: 3355   Special Judge

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

Source

分析:2-SAT问题,先搞清楚每个点拆成什么.对于第i对夫妇,i*2表示丈夫坐在新娘这一边,i*2+1表示妻子坐在新娘这一边.这样拆点的话,每对夫妇内部是不需要连边的,因为i*2和i*2+1是互相矛盾的,两个只能取一个.而并不是取了i*2就要取i*2对立的那一个.这是当初令我比较困惑的一个问题.
          主要的就是处理有矛盾的组合了.如果第i对的丈夫坐在了新娘对面,他不能和第j对的妻子坐在同一边,那么它肯定是和第j对的丈夫坐在同一边,以此类推,利用矛盾关系连边.
          最后要输出方案,常用的办法是对缩点后的图反向连边,跑一次拓扑排序,先到达的强连通分量为真,对立的强连通分量为假.
#include <cstdio>
#include <stack>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ;
int n, m, head[maxn], to[maxn], id[maxn], nextt[maxn], tot = , scc[maxn], pre[maxn], low[maxn], dfs_clock, cnt;
int head2[maxn], to2[maxn], nextt2[maxn], tot2 = , du[maxn], ok[maxn];
stack <int> s;
bool flag = true; void init()
{
memset(head, , sizeof(head));
memset(id, , sizeof(id));
tot = ;
memset(scc, , sizeof(scc));
memset(pre, , sizeof(pre));
memset(low, , sizeof(low));
dfs_clock = cnt = ;
memset(head2, , sizeof(head2));
tot2 = ;
memset(ok, , sizeof(ok));
memset(du, , sizeof(du));
flag = true;
} void add(int x, int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void add2(int x, int y)
{
to2[tot2] = y;
nextt2[tot2] = head2[x];
head2[x] = tot2++;
} void tarjan(int u)
{
s.push(u);
pre[u] = low[u] = ++dfs_clock;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!pre[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else
if (!scc[v])
low[u] = min(low[u], pre[v]);
}
if (low[u] == pre[u])
{
cnt++;
while ()
{
int t = s.top();
s.pop();
scc[t] = cnt;
if (t == u)
break;
}
}
} void topo()
{
queue <int> q;
for (int i = ; i <= cnt; i++)
if (!du[i])
q.push(i);
while (!q.empty())
{
int u = q.front();
q.pop();
if (!ok[u])
{
ok[u] = ;
ok[id[u]] = ;
for (int i = head2[u]; i; i = nextt2[i])
{
int v = to2[i];
if ((--du[v]) == )
q.push(v);
}
}
}
} int main()
{
while (scanf("%d%d", &n, &m) == && (n || m))
{
init();
for (int i = ; i <= m; i++)
{
int a, b;
char c, d;
scanf("%d%c %d%c", &a, &c, &b, &d);
add(c == 'h' ? a * + : a * , d == 'h' ? b * : b * + );
add(d == 'h' ? b * + : b * , c == 'h' ? a * : a * + );
}
add(, );
for (int i = ; i < n * ; i++)
if (!pre[i])
tarjan(i);
for (int i = ; i < n; i++)
{
if (scc[i * ] == scc[i * + ])
{
flag = false;
break;
}
id[scc[i * ]] = scc[i * + ];
id[scc[i * + ]] = scc[i * ];
}
if (!flag)
puts("bad luck");
else
{
for (int i = ; i < * n; i++)
{
for (int j = head[i]; j; j = nextt[j])
{
int v = to[j];
if (scc[v] != scc[i])
{
add2(scc[v], scc[i]);
du[scc[i]]++;
}
}
}
topo();
for (int i = ; i < n; i++)
{
if (ok[scc[i * ]] == )
printf("%dh ", i);
else
printf("%dw ", i);
}
printf("\n");
}
} return ;
}

poj3648 Wedding的更多相关文章

  1. POJ3648 Wedding 【2-sat】

    题目 Up to thirty couples will attend a wedding feast, at which they will be seated on either side of ...

  2. POJ-3648 Wedding 2sat

    题目链接:http://poj.org/problem?id=3648‘ 题意:一对情人举行婚礼,有n对夫妇参加,别人对着坐在一个长桌子的两边,新娘和新郎坐在最后面,新娘只能看见坐在他对面的人.现在, ...

  3. POJ3648:Wedding——题解(配2-SAT简易讲解)

    http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在 ...

  4. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  5. poj3648

    poj3648 题意 有一对新人结婚,n-1对夫妇去参加婚礼.有一个很长的座子,新娘与新郎坐在座子的两边(相反).接下来n-1对夫妇就坐,其中任何一对夫妇都不能坐在同一边,且(有一些人有奸情)这些有奸 ...

  6. NodeJs 学习笔记(一)Wedding 项目搭建

    说明:Ubuntu16.04 自带的NodeJs版本太低,安装包更新不了,只能编译安装了 一.NodeJs编译安装 下载:https://nodejs.org/en/download/ 修改目录权限: ...

  7. Wedding (poj 3648 2-SAT 输出随意一组解)

    Language: Default Wedding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9004   Accept ...

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

    Description Up to thirty couples will attend a wedding feast, at which they will be seated on either ...

  9. POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)

    Wedding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10427   Accepted: 3170   Specia ...

随机推荐

  1. JVM监控及堆栈内存

    jconsole 堆内存:存放new出来的对象 栈内存:存放基本数据结构和对象的引用,但对象本身放在堆中

  2. socket编程 123

    1. 预备知识 一直以来很少看到有多少人使用php的socket模块来做一些事情,大概大家都把它定位在脚本语言的范畴内吧,但是其实php的socket模块可以做很多事情,包括做ftplist,http ...

  3. ORA-01747

    java.sql.SQLException: ORA-01747: user.table.column, table.column 或列说明 语法中多了逗号 或者字段使用关键字

  4. Android:有关下拉菜单导航的学习(供自己参考)

    Android:有关==下拉菜单导航==的学习 因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写. 内容: ==下拉菜单导航== 学习网站:android Spinner控件详解 ...

  5. c# 画一个报告

    填充控件的grafics属性: private void printReportPage(object sender, System.Drawing.Printing.PrintPageEventAr ...

  6. PHP 函数总结

    感觉对函数了解的不够深,从头到尾梳理一遍(更新中....) 1,class_exists(),interface_exists(),method_exists(),get_class(),get_pa ...

  7. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  8. HDU 5183 Negative and Positive (NP) 前缀和+哈希

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  9. 【SSH框架】之Struts2系列(一)

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系 1.Struts2框架概述 (1).什么是Struts2 Struts2是一种基于MVC模式的轻量 ...

  10. ACM解题之在线翻译 Give Me the Number

    Give Me the Number Time Limit: 2 Seconds                                     Memory Limit: 65536 KB  ...