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 ...
随机推荐
- 第七次ScrumMeeting博客
第七次ScrumMeeting博客 本次会议于11月1日(三)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的 ...
- Scrum立会报告+燃尽图(Beta阶段第二周第四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2412 项目地址:https://coding.net/u/wuyy694 ...
- 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem F. Format
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:1s 空间限制:512MB 题目大意: 给定一个字符串,使用%[...] ...
- UVA 11270 轮廓线
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33787 题意: 用1*2或2*1的长条把n*m方格铺满的方案数. ...
- CDOJ ABCDE dp(前缀和优化)
题目链接: http://acm.uestc.edu.cn/#/problem/show/1307 ABCDE Time Limit: 1000/1000MS (Java/Others)Memory ...
- php获取指定div内容
<?php $p="http://127.0.0.1:8080/website/index.html"; $ch = curl_init(); curl_setopt($ch ...
- 为何php curl post模式发送数据速度变慢了?我来说说原因
事例: 今天要向一台服务器上传文件,原版是curl的get模式,现在改用了post模式,按照原本的思想,代码如下 <?php $post['c'] = 'config'; $post['t'] ...
- Java Map获取key和value 以及String字符串转List方法
一.问题描述 这里描述两个问题: 1.Java Map获取key和value的方法: 2.String字符串转List的方法: 二.解决方法 1.Java Map获取key和value的方法 2. ...
- jquery 添加与删除的规律 当要添加时候要定位到自己的父元素 当要删除时候 通过事件函数传入的this找到自己的父元素进行删除
jquery 添加与删除的规律 当要添加时候要定位到自己的父元素 当要删除时候 通过事件函数传入的this找到自己的父元素进行删除
- HDU5266-pog loves szh III
题目 给出一棵\(n\)个点的树,从1到\(n\)编号,\(m\)次询问\({LCA} _{v\in[L,R]}\). \(n,m\le 3\times 10^5\) 分析 我的做法是直接对LCA进 ...