题目

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".

输入样例

10 6

3h 7h

5w 3w

7h 6w

8w 3w

7h 3w

2w 5h

0 0

输出样例

1h 2h 3w 4h 5h 6h 7h 8h 9h

题解

题意:有n对夫妻坐成两排,其中0号为新娘新郎。有m对奸情,新娘不愿意看到对面排存在奸情,问合理方案

每对夫妻只有两种坐法,要么和新郎一排,要么和新娘一排

每对奸情不能同时在新郎一排

为了方便,我们选择以是否和新郎一排座位标准

建图:

首先新娘向新郎连边,表示新娘不可能和新郎一排

对于每对奸情a和b,我们设op[a]表示a的另一半

那么a->op[b],b->op[a],即a在新郎这侧则b的另一半必须在新郎这侧,b同理

按照我们之前的论断,Scc编号较小的被选择和新郎坐

那么另一半就和新娘坐

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 1005,maxm = 1000005,INF = 1000000000;
char opt;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
opt = c;
return out * flag;
}
int n,m,h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;}
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,top,cnt;
void dfs(int u){
dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
if (!dfn[to = ed[k].to]){
dfs(to);
low[u] = min(low[u],low[to]);
}else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
scci++;
do{Scc[st[top]] = scci;}while (st[top--] != u);
}
}
void init(){
memset(h,0,sizeof(h));
memset(Scc,0,sizeof(Scc));
memset(dfn,0,sizeof(dfn));
cnt = scci = top = 0; ne = 1;
}
int main(){
while (~scanf("%d%d",&n,&m)){
if (!n && !m) break;
init();
int a,b;
build(1,0);
while (m--){
a = 2 * read(); if (opt == 'w') a ^= 1;
b = 2 * read(); if (opt == 'w') b ^= 1;
build(a,b ^ 1); build(b,a ^ 1);
}
for (int i = 0; i < (n << 1); i++) if (!dfn[i]) dfs(i);
bool flag = true;
for (int i = 0; i < n; i++)
if (Scc[i << 1] == Scc[i << 1 | 1]){
flag = false; break;
}
if (!flag) puts("bad luck");
else {
for (int i = 1; i < n; i++)
if (Scc[i << 1] > Scc[i << 1 | 1]) printf("%dh ",i);
else printf("%dw ",i);
puts("");
}
}
return 0;
}

POJ3648 Wedding 【2-sat】的更多相关文章

  1. 【WebApi系列】浅谈HTTP

    [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi如何传递参数 [04]详解WebApi测试和PostMan [05]浅谈WebApi Core ...

  2. 【WebApi系列】浅谈HTTP在WebApi开发中的运用

    WebApi系列文章 [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi参数的传递 [04]详解WebApi测试和PostMan [05]浅谈W ...

  3. 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型

    前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...

  4. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】

    Spring Cloud(三):服务提供与调用 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇文章我们介绍了 Eureka 服务 ...

  5. 【插头DP】BZOJ1187- [HNOI2007]神奇游乐园

    [题目大意] 在n*m的网格中选一条回路,使权值和最大. [思路] 和之前裸的插头DP差不多,只不过现在回路不需要经过所有的格子.所以有以下几个注意点(具体看注释): (1)left和up插头相等的时 ...

  6. zz【清华NLP】图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐

    [清华NLP]图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐 图神经网络研究成为当前深度学习领域的热点.最近,清华大学NLP课题组Jie Zhou, Ganqu Cui, Zhengy ...

  7. 【故障解决】OGG-00446 错误解决

    [故障解决]OGG-00446 Could not find archived log for sequence 一.1  BLOG文档结构图       一.2  前言部分   一.2.1  导读和 ...

  8. 【故障解决】ORA-06502错误解决

    [故障解决]ORA-06502: PL/SQL: numeric or value error: character string buffer too small 一.1  BLOG文档结构图   ...

  9. 【Linux题目】第六关

    [定时任务规则] 1. 如果在某用户的crontab文件中有以下记录,该行中的命令多久执行一次(RHCE考试题)?( ) 30 4 * * 3 mycmd A. 每小时. B. 每周. C. 每年三月 ...

随机推荐

  1. FETCH - 用游标从查询中抓取行

    SYNOPSIS FETCH [ direction { FROM | IN } ] cursorname where direction can be empty or one of: NEXT P ...

  2. AngularJs学习笔记-组件间通讯

    组件间通讯 (1)输入属性@Input Tips:子组件属性的改变不会影响到父组件 如下,子组件中stockCode属性发生变化不会引起父组件stock属性的变化 (2)输入属性@Output 子组件 ...

  3. install cmake,install torch7

    cmake http://blog.csdn.net/jesse__zhong/article/details/21290675 torch7 http://wanghaitao8118.blog.1 ...

  4. vbs自由选择启动bat文件

    1.首先创建一个文件夹用来放bat文件和配置文件. 2.然后在bat文件中写入启动程序.中间红色框是启动程序,外面程序是用来隐藏命令提示符的. 3.txt配置文件配置vbs启动项,vbs只能找到此文件 ...

  5. mysql添加、移除服务

    sc delete 服务名 路径/bin/mysqld --install 服务名

  6. Uva 派 (Pie,NWERC 2006,LA 3635)

    依然是一道二分查找 #include<iostream> #include<cstdio> #include<cmath> using namespace std; ...

  7. shell基础及变量符号

    kernel主要的功能: 1.内存的管理 2.设备驱动程序 3.文件系统的管理 4.进程的管理 5.网络系统   vim /etc/profile.d/ profile(主配置文件) .d(子配置文件 ...

  8. CentOS 7 bonding模式双网卡绑定

    1.1    查看系统内核是否支持bonding [root@cobbler-node1 ~]# cat /boot/config-3.10.0-693.el7.x86_64 | grep -i bo ...

  9. PHP计算两个字符的相似程度similar_text

    在网站开发中,我们会常常要计算两个字符的相似程度,那么PHP为我们提供了一个函数similar_text;  1,similar_text的用法 如果我想计算"ly89cn"和&q ...

  10. JZOJ 4743. 积木

    Description Input Output Sample Input 38 7 63 9 41 10 5 Sample Output 18