POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 10427 | Accepted: 3170 | Special Judge |
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
题目链接:POJ 3648
题目讲的不是很清楚,实际上如下图所示
其中0号新婚夫妻是固定的了即0w在左0h在右,这题我是先把每一个夫妻拆成妻子和丈夫,然后两者均有两种状态,妻子在左或右,丈夫在左或右,因此可以拆成4*N个点,然后记在左符号为无,在右为$\lnot$,建图后跑2-SAT,首先由于固定的0号夫妻,因此0w在左、0h在右的情况必选,根据离散数学的公式可以得到有向边$<\lnot 0w, 0w>与<0h, \lnot 0h>$,然后然后任意一对夫妻不能坐在同一侧,有:$\lnot (a \land b) \land \lnot (\lnot a \land \lnot b)$,这样得到四条有向边:$<a,\lnot b><b, \lnot a><\lnot a, b><\lnot b, a>$
再然后根据每一对通奸的a与b,显然a与b不能同时出现在0w的右侧,即$\lnot (\lnot a \land \lnot b)=a \lor b$,因此得到两条有向边$<\lnot a, b>$与$<\lnot b, a>$,建图跑2-SAT后由于0w必选,因此mark[0w]是为1的,又由于mark为1的点在方案内,因此把mark为1的点都选出来并判断输出即可。
DFS输出最小字典序方案的代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen("name","r",stdin)
#define fout(name) freopen("name","w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 35 << 2;
const int M = 60 * 60 * 2 + 2 + 4 * 35;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[M];
int head[N], tot;
int st[N], top;
int vis[N];
int n, m; void init()
{
CLR(head, -1);
tot = 0;
CLR(vis, 0);
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
int rev(int i)
{
return i ^ 1;
}
int dfs(int u)
{
if (vis[rev(u)])
return 0;
if (vis[u])
return 1;
vis[u] = 1;
st[top++] = u;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!dfs(v))
return 0;
}
return 1;
}
int check(int n)
{
for (int i = 0; i < (n << 1); i += 2)
{
top = 0;
if (!vis[i] && !vis[rev(i)] && !dfs(i))
{
while (top)
vis[st[--top]] = 0;
if (!dfs(rev(i)))
return 0;
}
}
return 1;
} int main(void)
{
int i;
while (~scanf("%d%d", &n, &m) && (n || m))
{
init();
add(rev(0 << 2), 0 << 2);
add((0 << 2) + 2, rev((0 << 2) + 2));
for (i = 1; i < n; ++i) //4n
{
add(4 * i, rev(4 * i + 2));
add(4 * i + 2, rev(4 * i));
add(rev(4 * i), 4 * i + 2);
add(rev(4 * i + 2), 4 * i);
}
int u, v;
char su, sv;
for (i = 0; i < m; ++i) //2m
{
scanf(" %d%c %d%c", &u, &su, &v, &sv);
u <<= 2;
v <<= 2;
if (su == 'h')
u += 2;
if (sv == 'h')
v += 2;
add(rev(u), v);
add(rev(v), u);
}
if (!check(n << 1))
puts("bad luck");
else
{
for (int i = 4; i < (n << 2); i += 4)
printf("%d%c ", i >> 2, vis[i] ? 'w' : 'h');
puts("");
}
}
return 0;
}
Tarjan代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen("name","r",stdin)
#define fout(name) freopen("name","w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 35 << 2;
const int M = 60 * 60 * 2 + 2 + 4 * 35;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[M];
int head[N], tot;
int dfn[N], low[N], belong[N], st[N], top, ts, sc;
int ins[N];
int n, m; void init()
{
CLR(head, -1);
tot = 0;
CLR(dfn, 0);
CLR(low, 0);
CLR(belong, 0);
top = ts = sc = 0;
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
int rev(int i)
{
return i ^ 1;
}
void scc(int u)
{
dfn[u] = low[u] = ++ts;
ins[u] = 1;
st[top++] = u;
int i, v;
for (i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!dfn[v])
{
scc(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u])
{
++sc;
do
{
v = st[--top];
ins[v] = 0;
belong[v] = sc;
} while (u != v);
}
}
int main(void)
{
int i;
while (~scanf("%d%d", &n, &m) && (n || m))
{
init();
add(rev(0 << 2), 0 << 2);
add((0 << 2) + 2, rev((0 << 2) + 2));
for (i = 1; i < n; ++i) //4n
{
add(4 * i, rev(4 * i + 2));
add(4 * i + 2, rev(4 * i));
add(rev(4 * i), 4 * i + 2);
add(rev(4 * i + 2), 4 * i);
}
int u, v;
char su, sv;
for (i = 0; i < m; ++i) //2m
{
scanf(" %d%c %d%c", &u, &su, &v, &sv);
u <<= 2;
v <<= 2;
if (su == 'h')
u += 2;
if (sv == 'h')
v += 2;
add(rev(u), v);
add(rev(v), u);
}
for (i = 0; i < (n << 2); ++i)
if (!dfn[i])
scc(i);
int flag = 1;
for (i = 0; i < (n << 2) && flag; i += 2)
if (belong[i] == belong[i ^ 1])
flag = 0;
if (!flag)
puts("bad luck");
else
{
for (int i = 4; i < (n << 2); i += 4)
printf("%d%c ", i >> 2, belong[i] < belong[i ^ 1] ? 'w' : 'h');
puts("");
}
}
return 0;
}
POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)的更多相关文章
- poj 3648 Wedding 2-SAT问题入门题目
Description Up to thirty couples will attend a wedding feast, at which they will be seated on either ...
- 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)
题目链接 题意看这吧..https://www.cnblogs.com/wenruo/p/5885948.html \(Solution\) 每对夫妇只能有一个坐在新娘这一边,这正符合2-SAT初始状 ...
- 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 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- 【POJ】3648 Wedding
http://poj.org/problem?id=3648 题意:n对人(编号0-n-1,'w'表示第一个人,'h'表示第二个人),每对两个,人坐在桌子两侧.满足:1.每对人中的两个人不能坐在同一侧 ...
随机推荐
- 【洛谷1993】小K的农场(差分约束系统模板题)
点此看题面 大致题意: 给你若干组不等式,请你判断它们是否有解. 差分约束系统 看到若干组不等式,应该很容易想到差分约束系统吧. \(A-B≥C\):转换可得\(A-B≥C\) \(A-B≤C\):转 ...
- SEO人士一定要了解的搜索引擎惩罚原则
SEO人士一定要了解的搜索引擎惩罚原则 SEO 的人一般都知道SEO分为白帽,黑帽,甚至还有灰帽.简单说,如果你熟读过谷歌网站质量指南,就可以了解,符合搜索引擎质量规范并且符合用户体验的SEO ...
- 解决wget下载https时报错 --no-check-certificate (不检查证书)
如果使用 wget下载https开头的网址域名 时报错,你需要加上 --no-check-certificate (不检查证书)选项 例如: wget https://pypi.python.org/ ...
- 我的Linux学习之路的感悟
首先要跟大家说声抱歉,这么久一直没有更新,有负大家对我的期望. 半年的Linux运维的学习到目前已工作一个月零9天,这一路走来的艰辛和挣扎只有自己最清楚. 首先要感谢公司的同事的宽容接纳和耐心指点.感 ...
- SpringBoot学习记录(一)
一.SpringBoot入门 1.SpringBoot简介 简化Spring应用开发的一个框架:整个Spring技术栈的一个大整合:J2EE开发的一站式解决方案: SpringBoot的优点: (1) ...
- PHP中可变变量到底有什么用?
转自:http://blog.csdn.net/engine_1124/article/details/8660291 什么是可变变量? PHP提供了一种其他类型的变量——可变变量.可变变量允许我们动 ...
- C# 文件操作 常用的类
File------实用类,提供许多静态方法,用于移动.删除.和复制文件. Directory------实用类,提供许多静态方法,用于移动.删除和复制目录. Path------ 实用类,用于处理路 ...
- vbs自由选择启动bat文件
1.首先创建一个文件夹用来放bat文件和配置文件. 2.然后在bat文件中写入启动程序.中间红色框是启动程序,外面程序是用来隐藏命令提示符的. 3.txt配置文件配置vbs启动项,vbs只能找到此文件 ...
- 51nod——2478 小b接水(预处理 思维)
我本来想把每个谷都处理了,想了下觉得不好办.后来看其他人写的是处理每个位置,把每个位置可以接的水累加起来.整挺好. #include <bits/stdc++.h> using names ...
- 5.7 并行复制配置 基于GTID 搭建中从 基于GTID的备份与恢复,同步中断处理
5.7 并行复制配置 基于GTID 搭建中从 基于GTID的备份与恢复,同步中断处理 这个文章包含三个部分 1:gtid的多线程复制2:同步中断处理3:GTID的备份与恢复 下面文字相关的东西 大部分 ...