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.每对人中的两个人不能坐在同一侧 ...
随机推荐
- 使用 Repeater方式和完全静态页面使用AJAX读取和提交数据
1.使用Repeater方式: Comments.aspx <html xmlns="http://www.w3.org/1999/xhtml"> <head r ...
- AngularJS 历经实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- java web用户登录界面
做这次实验,主要用到了mysql java web 的 内容 实验代码: IUserDao.java package com.jaovo.msg.dao; import java.util.List ...
- c++question 004 c++基本数据类型有哪些?
(1)signed int类型 整数型 占内存4个字节 一个字节byte 占8个二进制位 一个整型就占32位 (2)short int 短整型 占内存2个字节 一个短整型占16位 (3)long i ...
- HTML5/CSS3 第二章页面组件
页面组件 1 元素的尺寸/边框/背景 1.1 css尺寸相关属性 height 高度 min-height 最小高度 max-height 最大高度 width 宽度 min-width 最小宽度 m ...
- 【Ecshop】后台菜单与权限管理
主要php文件: 1,admin/includes/inc_menu.php ECSHOP管理中心菜单数组--配置菜单组及URL 2,languages/zh_cn/admin/common.php ...
- PHP导出成PDF功能开发教程
准备工作 首先查询了相关的类库,有FPDF,zendPDF,TcPDF等等.首先看了下先选择了FPDF,可以说除了中文字符以外没有什么问题,中文乱码而且看了下最新版本没有很好的解决方案,所以只能放弃. ...
- tcl之string操作-match/map/大小写转换
- 数据分析处理库Pandas——概述
导入Pandas库 创建DataFrame结构 读取.csv文件 titanic_train.csv文件:https://files.cnblogs.com/files/gloria-zhang/ti ...
- 基于django的个人博客网站建立(二)
基于django的个人博客网站建立(二) 前言 网站效果可点击这里访问 今天主要完成后台管理员登录的状态以及关于文章在后台的处理 具体内容 首先接上一次内容,昨天只是完成了一个登录的跳转,其他信息并没 ...