博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3648 Wedding 【2-sat】
阅读量:5316 次
发布时间:2019-06-14

本文共 3411 字,大约阅读时间需要 11 分钟。

题目

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
#include
#include
#include
#include
#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<
<<' '; 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;}

转载于:https://www.cnblogs.com/Mychael/p/8289035.html

你可能感兴趣的文章
mongo学习笔记(二):聚合,游标
查看>>
复选框checked 选中后不显示打钩
查看>>
[Halcon] 算子学习_Calibration_Calibration Object
查看>>
Jupyter notebook: TypeError: __init__() got an unexpected keyword argument 'io_loop 问题
查看>>
Python之OS模块
查看>>
javascript keycode大全 转
查看>>
VMWare 安装 Linux
查看>>
Hbase笔记4 java操作Hbase
查看>>
CentOS 安装jdk1.7 64位
查看>>
一对经典的时间获取客户/服务器程序
查看>>
Android学习笔记(1)
查看>>
received packet with own address as source address
查看>>
java enum分析
查看>>
树莓派 Raspberry Pi 更换国内源
查看>>
数位DP模板
查看>>
day01基础部分
查看>>
bzoj 1024 [ SCOI 2009 ] 生日快乐 —— 递归
查看>>
excel导入 HSSFWorkbook和XSSFWorkbook
查看>>
tarjan算法详解
查看>>
常用模块之 time,datetime,random,os,sys
查看>>