poj3648 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
#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的更多相关文章
- POJ3648 Wedding 【2-sat】
题目 Up to thirty couples will attend a wedding feast, at which they will be seated on either side of ...
- POJ-3648 Wedding 2sat
题目链接:http://poj.org/problem?id=3648‘ 题意:一对情人举行婚礼,有n对夫妇参加,别人对着坐在一个长桌子的两边,新娘和新郎坐在最后面,新娘只能看见坐在他对面的人.现在, ...
- POJ3648:Wedding——题解(配2-SAT简易讲解)
http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在 ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- poj3648
poj3648 题意 有一对新人结婚,n-1对夫妇去参加婚礼.有一个很长的座子,新娘与新郎坐在座子的两边(相反).接下来n-1对夫妇就坐,其中任何一对夫妇都不能坐在同一边,且(有一些人有奸情)这些有奸 ...
- NodeJs 学习笔记(一)Wedding 项目搭建
说明:Ubuntu16.04 自带的NodeJs版本太低,安装包更新不了,只能编译安装了 一.NodeJs编译安装 下载:https://nodejs.org/en/download/ 修改目录权限: ...
- Wedding (poj 3648 2-SAT 输出随意一组解)
Language: Default Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9004 Accept ...
- poj 3648 Wedding 2-SAT问题入门题目
Description Up to thirty couples will attend a wedding feast, at which they will be seated on either ...
- POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)
Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10427 Accepted: 3170 Specia ...
随机推荐
- 【TCP_协议_socket接口】-jmeter
1.ip 2.端口号 3.传入参数 4.告诉软件返回 最后以为是什么,不然就会报错 或者无限制的等待 查ascll 码表 启动接口的方法
- centos 6.5 双网卡 上网 virtualbox nat hostonly
虚拟机两张网卡:分别调成NAT(eth0)和host only(eht1)模式. nat的网卡不用设置,host only网卡调为(vi /etc/sysconfig/network-scripts/ ...
- 如何通俗理解贝叶斯推断与beta分布?
有一枚硬币(不知道它是否公平),假如抛了三次,三次都是“花”: 能够说明它两面都是“花”吗? 1 贝叶斯推断 按照传统的算法,抛了三次得到三次“花”,那么“花”的概率应该是: 但是抛三次实在太少了,完 ...
- 机器学习之决策树(ID3)算法
最近刚把<机器学习实战>中的决策树过了一遍,接下来通过书中的实例,来温习决策树构造算法中的ID3算法. 海洋生物数据: 不浮出水面是否可以生存 是否有脚蹼 属于鱼类 1 是 是 是 2 ...
- ES6的新特性(10)——Class 的基本语法
Class 的基本语法 简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y ...
- C++ STL 全排列
摘自爱国师哥博客https://www.cnblogs.com/aiguona/p/7304945.html 一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元 ...
- 王者荣耀交流协会第三次Scrum立会
会议时间:2017年10月22号 18:00-18:32,时长32分钟. 会议地点:中快餐厅二楼第二排倒数第二个桌子. 立会内容: 1.每位同学汇报了今日工作. 2.通过讨论我们决定用存excel ...
- 软工网络15个人作业4--alpha阶段个人总结
一.个人总结 自我评价表 类别 具体技能和面试问题 现在的回答 毕业找工作 语言 最拿手的语言之一,代码量是多少 java,代码量大概两三千行吧 语言 最拿手的语言之二,代码量是多少 python,代 ...
- 3dContactPointAnnotationTool开发日志(二五)
记录一下当前进度:
- 使用union all 遇到的问题(俩条sql语句行数的和 不等于union all 后的 行数的和 !);遗留问题 怎么找到 相差的呐俩条数据 ?
create table buyer as SELECT b.id AS bankid FROM v_product_deal_main m, base_member b WHERE b.id = m ...