uva 11294
Problem E: Wedding
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.
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. 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
Possible Output for Sample Input
1h 2h 3w 4h 5h 6h 7h 8h 9h 2-sat
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector> using namespace std; const int MAX_N = ;
int N,M;
int low[ * MAX_N],pre[ * MAX_N],cmp[ * MAX_N];
vector<int> G[ * MAX_N];
stack<int> S;
int dfs_clock,scc_cnt;
int ans[MAX_N * ]; void dfs(int u) {
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
low[u] = min(low[u],low[v]);
} else if(!cmp[v]) {
low[u] = min(low[u],pre[v]);
}
} if(pre[u] == low[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} bool scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i < * N; ++i) if(!pre[i]) dfs(i); for(int i = ; i < * N; ++i) {
if(cmp[i] == cmp[ * N + i]) return false;
} return true;
} int main()
{
// freopen("sw.in","r",stdin);
while(~scanf("%d%d",&N,&M) && (N || M)) {
for(int i = ; i < * N; ++i) G[i].clear();
for(int i = ; i < N; ++i) {
G[i].push_back(i + N + * N);
G[i + * N].push_back(i + N);
G[i + N].push_back(i + * N);
G[i + * N].push_back(i);
}
G[ * N + N].push_back(N);
G[].push_back( * N); for(int i = ; i <= M; ++i) {
int u,v;
char ch1,ch2;
scanf("%d%c%d%c",&u,&ch1,&v,&ch2);
u += ch1 == 'h' ? N : ;
v += ch2 == 'h' ? N : ;
G[u].push_back(v + * N);
G[v].push_back(u + * N); } if(!scc()) {
printf("bad luck\n");
} else {
int len = ;
for(int i = ; i < N; ++i) {
if(cmp[i + * N] < cmp[i]) {
ans[len++] = i;
}
if(cmp[i + N + * N] < cmp[i + N]) {
ans[len++] = i + N;
}
} for(int i = ; i < len; ++i) {
printf("%d%c",ans[i] >= N ? ans[i] - N : ans[i],
ans[i] >= N ? 'h' : 'w');
printf("%c",i == len - ? '\n' : ' ' );
}
}
}
//cout << "Hello world!" << endl;
return ;
}
uva 11294的更多相关文章
- UVA 11294 - Wedding(Two-Set)
UVA 11294 - Wedding 题目链接 题意:有n对夫妻,0号是公主.如今有一些通奸关系(男男,女女也是可能的)然后要求人分配在两側.夫妻不能坐同一側.而且公主对面一側不能有两个同奸的人,问 ...
- UVA 11294 Wedding(2-sat)
2-sat.不错的一道题,学到了不少. 需要注意这么几点: 1.题目中描述的是有n对夫妇,其中(n-1)对是来为余下的一对办婚礼的,所以新娘只有一位. 2.2-sat问题是根据必然性建边,比如说A与B ...
- UVA 11294 Wedding
给n对夫妇安排座位,其中0h,0w分别表示新郎,新娘.同一对新郎,新娘不能坐在同一侧,而且互为通奸关系的人不能同时坐在新娘对面. 这道题目真是掉尽节操啊,,,欧美的氛围还是比较开放的. 分析: 首先说 ...
- UVa 11294 Wedding (TwoSat)
题意:有 n-1 对夫妻参加一个婚宴,所有人都坐在一个长长的餐桌左侧或者右侧,新郎和新娘面做面坐在桌子的两侧.由于新娘的头饰很复杂,她无法看到和她坐在同一侧餐桌的人,只能看到对面餐桌的人.任意一对夫妻 ...
- Wedding UVA - 11294(2-SAT男女分点)
题意: 有N-1对夫妻参加一个婚宴,所有人都坐在一个长长的餐桌左侧或者右侧,新郎和新娘面做面坐在桌子的两侧.由于新娘的头饰很复杂,她无法看到和她坐在同一侧餐桌的人,只能看到对面餐桌的人.任意一对夫妻不 ...
- Uva 11294 婚姻
题目链接:https://vjudge.net/contest/166461#problem/C 题意: n对夫妻,有m对人吵过架,不能排在同一边,求新娘的一边的人: 分析: 每对夫妻,看成两个点,女 ...
- UVA 11294 wedding 2-sat
可以把一对夫妇当成一个节点,然后拆点的话,h和w分别为真和假,然后直接按照题目中说的建图染色即可 #include <iostream> #include <cstdio> # ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
随机推荐
- [笔记]--Oracle 10g在Windows 32位系统使用2G以上内存
1.修改c:\boot.ini文件 打开boot.ini文件,我的电脑->属性->高级->启动和恢复->编辑,设置在最后一行末尾添加/PAE选项后如下: [boot loade ...
- 对Iframe和图表设置高度的优质代码
//对Iframe和图表设置高度 function f() { parent.window.setWinHeight(parent.window.document.getElementById(&qu ...
- 算法系列7《CVN》
计算CVN时使用二个64位的验证密钥,KeyA和KeyB. 1) 计算CVN 的数据源包括: 主账号(PAN).卡失效期和服务代码,从左至右顺序编排. 4123456789012345+8701+11 ...
- bzoj 3196/tyvj p1730 二逼平衡树
原题链接:http://www.tyvj.cn/p/1730 树套树... 如下: #include<cstdio> #include<cstdlib> #include< ...
- FPGA---ucf文件编写
摘要:本文主要通过一个实例具体介绍ISE中通过编辑UCF文件来对FPGA设计进行约束,主要涉及到的约束包括时钟约束.群组约束.逻辑管脚约束以及物理属性约束. Xilinx FPGA设计约束的分类 Xi ...
- 在使用SQLite插入数据时出现乱码的解决办法
在VC++中通过sqlite3.dll接口对sqlite数据库进行操作,包括打开数据库,插入,查询数据库等,如果操作接口输入参数包含中文字符,会导致操作异常.例如调用sqlite3_open打开数 ...
- 关于1>LINK : fatal error LNK1168: 无法打开 ....exe或者....dll进行写入的问题
我们用VS编译器运行我们的程序时候,可能会出现关于1>LINK : fatal error LNK1168: 无法打开 ...dll 进行写入或者是1>LINK : fatal err ...
- python中将字符串转化为本地变量
var = 123445s= locals()['var']s2=vars()['var'] print s,s2
- C++类中的this指针的作用
1.我们知道C++的类成员函数中,默认都隐含了一个this指针,标识调用该成员函数的对象 2.为什么需要有一个this指针呢?C++设计这个机制的初衷是什么呢? 我们知道,普通的C++类,其成员函数是 ...
- 【收藏】Linux下tomcat内存配置
常见的内存溢出有以下两种: java.lang.OutOfMemoryError: PermGen space java.lang.OutOfMemoryError: Java heap space ...