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. mysql数据导到本地

    需求: 把mysql查询结果导出到txt(其他格式亦可),放在本地,供下一步使用 首先网上查了下,select * from driver into outfile 'a.txt'; 前面是你的sql ...

  2. CentOS7.2 部署Haproxy 1.7.2

    原文发表于cu:2017-03-16 参考文档: haproxy:http://www.haproxy.org/ 本文涉及haproxy的安装,并做简单配置. 一.环境准备 1. 操作系统 CentO ...

  3. JAVA学习笔记--迭代器

    迭代器(Iterator)是一种设计模式.它是一个对象,它的工作是遍历并选择序列中的对象,而客户端程序员不必知道或关心该序列底层的结构.创建迭代器的代价小,因而迭代器通常被称为轻量级对象. 一.Ite ...

  4. 使用 Sublime Text 做 Javascript 编辑器 - 集成 JSHint 问题检测工具

    JSHint(jshint.com)是 Javascritp 代码质量工具,可以帮助开发人员发现 Javascript 代码中的错误和潜在的问题.jshint.com 是一个在线编辑器,我们可以为 S ...

  5. js 插件 issue

    1 iscroll 5 和 lazyload 同时使用  转自 yinjie //lazyload var $scrollEle = $("#wrapper") $("i ...

  6. Base64编码图片存取与前台显示

    需求:将Base64编码图片以BLOB类型存入数据库,需要时取出显示 后台: String base64str=new String(log.getRequest_imgdata());//log为实 ...

  7. The Bits (思维+找规律)

    Description Rudolf is on his way to the castle. Before getting into the castle, the security staff a ...

  8. firefox插件Firebug的使用教程

    什么是Firebug 从事了数年的Web开发工作,越来越觉得现在对WEB开发有了更高的要求.要写出漂亮的HTML代码:要编写精致的CSS样式表展示每个页面模块:要调试 javascript给页面增加一 ...

  9. App接口如何保证安全

    微信开发或者高德地图,百度地图什么的api要使用,使用之前都需要注册一个账号,然后系统会给你一个key,然后调用api的时候把key传给服务器. 平常公司内部开发项目时,直接用mvc为app客户端提供 ...

  10. WPF和Expression Blend开发实例:充分利用Blend实现一个探照灯的效果

    本篇文章阅读的基础是在读者对于WPF有一定的了解并且有WPF相关的编码经验,对于Blend的界面布局有基础的知识.文章中对于相应的在Blend中的操作进行演示,并不会进行细致到每个属性的介绍.同时,本 ...