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.每对人中的两个人不能坐在同一侧 ...
随机推荐
- Linux一键脚本合集vps
首先,想说说一键脚本流行的原因何在? 众所周知的是,Linux 是占据大半壁江山的服务器系统,但在桌面上的占有率可就远不是那么回事儿了,使用和熟悉 Linux 的人远没有 Windows 多,但又因为 ...
- 5.3 Date类型
创建一个日期对象: var now = new Date( ); var now= new Date(); document.write(now); //Tue Apr 19 2016 11:43:5 ...
- Java发出声卡蜂鸣生的方法
方法一: Toolkit.getDefaultToolkit().beep(); 方法二: System.out.println('\007');//八进制数
- graphQL 启动报错No method or field found with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument), in priority order:
-------------------root.graphqls---------------------------这个文件用来定义属性字段,必须和实体类相同 文件里面的字段写错会报这个错误 com ...
- MySql主从同步笔记
1.MySql主从同步是基于二进制日志实现的,二进制日志记录了主服务器数据库的所有变动,从服务器通过读取和执行该日志文件保持和主数据库的数据一致: 2.配置主服务器 a.开启二进制日志,找到MySql ...
- 六、Linux 文件基本属性
Linux 文件基本属性 Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规 ...
- 三十三、MySQL 导入数据
MySQL 导入数据 本章节我们为大家介绍几种简单的 MySQL 导出的数据的命令. 1.mysql 命令导入 使用 mysql 命令导入语法格式为: mysql -u用户名 -p密码 < 要导 ...
- k8s的pv和pvc简述
pvc:资源需要指定:1.accessMode:访问模型:对象列表: ReadWriteOnce – the volume can be mounted as read-write by a s ...
- 【CSS】简略说明css的权重之分
/*权重 :id > class > 标签 (小环境) 权重:内联 > 内部 > 外部 (大环境) 小环境处于内部环境中 */ <style> #p1{ /* id ...
- JZOJ 5773. 【NOIP2008模拟】简单数学题
5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms Memory Limits ...