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

这题题目题意简直可怕
有一对新人结婚,邀请n对夫妇去参加婚礼。
有一张很长的桌子,人只能坐在桌子的两边,还要满
足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇
之中可能有通奸关系(包括男男,男女,女女),有通
奸关系的不能同时坐在新娘的对面,可以分开坐,可以
同时坐在新娘这一侧。如果存在一种可行的方案,输出
与新娘同侧的人。

源地址 https://blog.csdn.net/u012915516/article/details/48442265

SAT这种问题 和网络流一样 只要图建对了 就对了 
主要是建图问题

特殊情况:当新郞有奸情的时候,与他有奸情的必需选择了(新浪在对面),

当新娘有奸情时候没关系,不处理。


 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack> using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 0x7ffffff;
struct node {
int v, next;
} edge[maxn];
int head[maxn], dfn[maxn], low[maxn];
int s[maxn], belong[maxn], instack[maxn];
int tot, cnt, top, flag, n, m;
void init() {
tot = cnt = top = flag = ;
memset(s, , sizeof(s));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
}
void add(int u, int v ) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int v) {
dfn[v] = low[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; ~i ; i = edge[i].next ) {
int j = edge[i].v;
if (!dfn[j]) {
tarjan(j);
low[v] = min(low[v], low[j]);
} else if (instack[j]) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v) ;
}
}
int ans[maxn];
int check() {
for (int i = ; i < * n ; i++)
if (!dfn[i]) tarjan(i);
for (int i = ; i < * n ; i += ) {
if (belong[i] == belong[i + ]) return ;
if (belong[i] < belong[i + ]) ans[i / ] = i;
else ans[i / ] = i + ;
}
return ;
}
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if (n == && m == ) break;
int t1, t2;
char c1, c2;
init();
memset(ans, , sizeof(ans));
for (int i = ; i < m ; i++) {
scanf("%d%c%d%c", &t1, &c1, &t2, &c2);
if (c1 == 'h') t1 = t1 * + ;
else t1 = t1 * ;
if (c2 == 'h') t2 = t2 * + ;
else t2 = t2 * ;
if (t1 == ) add(t2 ^ , t2);
else if (t2 == ) add(t1 ^ , t1);
else if (t1 == || t1 == ) {}
else {
add(t1 ^ , t2);
add(t2 ^ , t1);
}
}
if (check()) {
for (int i = ; i < n ; i++) {
if (ans[i] == i * ) printf("%dw ", i);
else printf("%dh ", i);
}
printf("\n");
} else printf("bad luck\n"); }
return ;
}

最新文章

  1. 随着ScrollView的滑动,渐渐的执行动画View
  2. MySQL的show语句大全
  3. Android 自定义Toast
  4. Unity中Instantiate一个prefab时需要注意的问题
  5. Fastjson Feathure
  6. CSS核心属性
  7. JPanel与JFrame的区别
  8. Ubuntu16下Hadoop安装
  9. 1004: [HNOI2008]Cards - burnside + DP
  10. SharePoint 读取内容的插件之SharepointPlus
  11. Linux学习笔记之九————ubuntu软件安装与卸载
  12. Zephyr的Power Management
  13. .net跨防火墙链接oracle连接池链接长时间无通讯数据被断开后报错问题解决
  14. marquee 的浏览器兼容性
  15. Java对象的克隆
  16. 使用 requests 访问 HTTPS
  17. [oracle复习] - Oracle
  18. MySQL语句相关
  19. MySQL知识总结(二)基本语句总结
  20. JS验证表单中TEXT文本框中是否含有非法字符

热门文章

  1. webstorm报错Unescaped xml character解决方案1
  2. 爬取多个url页面数据--手动实现
  3. 【异常】The server time zone value &#39;&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188;&#228;&#39; is unrecognized or represents more than one time zone.
  4. python-6面向对象编程
  5. 笔记-python-statement-with
  6. PHP.17-文本式留言板
  7. css3心形 perspective transform
  8. 对于STM32别名区的理解 (转载)
  9. 简洁好看的form样式收藏
  10. python语言中threading.Thread类的使用方法