题目

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. 启动Jmeter时遇到的几种错误

    1.权限不够 解决办法:用管理员权限运行 2.sdk版本太低 解决办法:1)查看当前sdk版本:java -version 2)安装sdk1.7或以上版本(jmeter3.0版本要用sdk1.7及以上 ...

  2. SQL Server中Table字典数据的查询SQL示例代码

    SQL Server中Table字典数据的查询SQL示例代码 前言 在数据库系统原理与设计(第3版)教科书中这样写道: 数据库包含4类数据: 1.用户数据 2.元数据 3.索引 4.应用元数据 其中, ...

  3. cf492E. Vanya and Field(扩展欧几里得)

    题意 $n \times n$的网格,有$m$个苹果树,选择一个点出发,每次增加一个偏移量$(dx, dy)$,最大化经过的苹果树的数量 Sol 上面那个互素一开始没看见,然后就GG了 很显然,若$n ...

  4. mysql 查询 7天内的数据

    SELECT ID,SERVICE FROM new_schedules_spider_full WHERE SERVICE = 'WSA2' and date_sub(curdate(), inte ...

  5. 5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)

    使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...

  6. LAMP 搭建练习

    目录 LAMP 搭建 1:CentOS 7, lamp (module): http + php + phpMyAdmin + wordpress 192.168.1.7 配置虚拟主机 xcache ...

  7. 二十五、MySQL 索引

    MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索 ...

  8. ethtool查看网卡以及修改网卡配置

    ethtool 命令详解 命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx       //查询ethx网口基本设置,其中 x 是对应网卡的编号,如et ...

  9. linux三剑客之sed深度实践

    参数: -a:追加文本到指定行后 -i:插入文本到指定行前 1.单行增加 [root@redhat~]#   sed  ' 2a  6,f '  linux.tet 1,a 2,b 6,f 3,c 4 ...

  10. Nginx读书笔记

    ... upstream proxy_svrs { server http://192.168.1.1:8001/uri/; server http://192.168.1.2:8001/uri/; ...