poj 3648 Wedding 2-SAT问题入门题目
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 ;
}
poj 3648 Wedding 2-SAT问题入门题目的更多相关文章
- POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)
Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10427 Accepted: 3170 Specia ...
- POJ.3648.Wedding(2-SAT)
题目链接 题意看这吧..https://www.cnblogs.com/wenruo/p/5885948.html \(Solution\) 每对夫妇只能有一个坐在新娘这一边,这正符合2-SAT初始状 ...
- POJ 3648 Wedding (2-SAT,经典)
题意:新郎和新娘结婚,来了n-1对夫妻,这些夫妻包括新郎之间有通奸关系(包括男女,男男,女女),我们的目地是为了满足新娘,新娘对面不能坐着一对夫妻,也不能坐着有任何通奸关系的人,另外新郎一定要坐新娘对 ...
- POJ 3648 Wedding
2-SAT,直接选择新娘一侧的比较难做,所以处理的时候选择新郎一侧的,最后反着输出就可以. A和B通奸的话,就建边 A->B'以及B->A’,表示 A在新郎一侧的话,B一定不在:B在新郎一 ...
- POJ - 3648 Wedding (2-SAT 输出解决方案)
题意:有N-1对夫妇和1对新郎新娘要出席婚礼,这N对人要坐在走廊两侧.要求每对夫妇要坐在不同侧.有M对人有通奸关系,对于这一对人,不能同时坐在新娘对面(新娘新郎也可能和别人有通奸关系).求如何避免冲突 ...
- poj 3648 Wedding【2-SAT+tarjan+拓扑】
看错题*n,注意是输出新娘这边的-- 按2-SAT规则连互斥的边,然后注意连一条(1,1+n)表示新娘必选 然后输出color[belong[i]]==color[belong[1+n(新娘)]]的点 ...
- poj 2186 强连通入门题目
每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...
- poj 1741 树的点分治(入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18205 Accepted: 5951 Description ...
- 树形DP入门题目推荐以及解析
关于树形DP几道入门题目 今天恶补树形DP,感觉海星. 其实挺简单的. 介绍几道例题,我会的. 1.洛谷P1352 没有上司的舞会 我的一篇题解 我们可以考虑每一个节点都是有两种情况. 一个是被邀请: ...
随机推荐
- Intellij IDEA 查找接口实现类的快捷键
查找接口的实现类: IDEA 风格 ctrl + alt +B 查看类或接口的继承关系: ctrl + h 1.IDEA_查找接口的实现 的快捷键 http://blog.csdn.net/u0100 ...
- SSH 登录时出现如下错误:No supported key exchange algorithms
https://help.aliyun.com/knowledge_detail/41486.html
- IntelliJ IDEA 2016 汉化说明:
把汉化包解压后,然后将resources_cn.jar 复制到 .lib 目录,重新打开就是中文.
- composer安装教程(Linux版)
composer 是款不错的工具,那么如何进行安装composer呢 如果您是linux系统或是mac系统 请先确定是否安装了curl linux安装curl 1 yum install -y c ...
- zookeeper集群(二)
经过前一篇文章<zookeeper伪集群一>的阅读,相信大家对zookeeper集群已经有一定的了解了,接下来我们再谈谈zookeeper真集群.其实真集群和伪集群还是有很多相似的部分的, ...
- Python学习笔记:logging(日志处理)
在一个软件中,日志是可以说必不可少的一个组成部分,通常会在定位客户问题或者记录软件使用情况等场景中会用到.logging模板块是Python的一个内置标准库,用于实现对日志的控制输出,对于平常的日志输 ...
- 洛谷P1451 求细胞数量
求细胞数量 题目链接 这道题大概是一个最简单的联通块的题了qwq 注意枚举起点的时候 一定不要从0开始不然你就会从0进入到了其他联通块中从而多查. 一定看清题意这道题不是同色为联通块!!! AC代码如 ...
- nodejs基础1
nodejs学习网站: https://github.com/alsotang/node-lessons 1.全局对象 (1)node中没有window对象,有global对象替代window对象 g ...
- JSONP解决跨域完整例子
1.这个案例是仿照百度搜索,输入关键词,会出现下拉菜单的过程. 效果: 2.具体做法: (1)利用百度的数据库做script标签的src. 复制之后的地址是这样的 https://sp0.baidu. ...
- ITIBB原创,互联网首部自媒体小说《1024伐木累》-小白篇之入职-总章节一
小序 IT人不懂爱?代码汪是小白?又有谁,懂我情怀? 逗比青年,背上行囊,懵懵懂懂闯帝都!前途似海,来日方长! 青春无梦妄少年!认定就作,不平就说,碰撞火花,如此绚烂…… IT人有比格?其实,那是顽强 ...