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.每对人中的两个人不能坐在同一侧 ...
随机推荐
- 获取页面URL参数值
JavaScript function GetParams(urlAddress) { var i, strLength, str, keyName, keyValue, params = {}, u ...
- psutil——获取系统信息的Python第三方模块
本文摘自廖雪峰大神个人网站:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984 用Python来编写脚本简化日常的运维 ...
- stm32基本定时器timer6的原理与使用
/********************基本定时器 TIM 参数定义,只限 TIM6.7************/ /* 一.定时器分类 STM32F1 系列中,除了互联型的产品,共有 8 个定时器 ...
- A1031 Hello World for U (20)(20 分)
A1031 Hello World for U (20)(20 分) Given any string of N (>=5) characters, you are asked to form ...
- Hive 分析函数lead、lag实例应用
Hive的分析函数又叫窗口函数,在oracle中就有这样的分析函数,主要用来做数据统计分析的. Lag和Lead分析函数可以在同一次查询中取出同一字段的前N行的数据(Lag)和后N行的数据(Lead) ...
- layer父页面调用子页面的方法
由于不知道如何在子页面获取到layer定义的确定按钮,于是就在子页面上定义了一个方法,然后在由父页面在点确定按钮时调用子页面所定义的这个方法,从而执行子页面方法里面的内容: 子页面代码: functi ...
- easyui tree datagrid动态添加表头和表格数据,动态弹出框,修改和删除按钮
1.要有获取表头的URL和表格的URL 背景:点击树的一个节点,就加载一个表格,这个表格是动态的,表头和表格数据都是动态的 解决方案:需要两个URL,一个是获取表头的URL,一个是获取表格数据的URL ...
- 关于tree节点的刷新
1.刷新节点分为刷新整个树和刷新指定节点 (1)刷新整个树 $("#tree").tree("reload"); (2)刷新指定节点(方法:传入需要刷新节点的父 ...
- TortoiseSVN 同时检索多人的提交日志记录(如图操作)
- ogre3D学习基础19 --- 材质的继承,纹理的滚动与旋转
以上一节为基础,废话不多说. 首先新增一个节点,用于比较显示 //新增一个节点 ent = mSceneMgr->createEntity("Quad"); ent-> ...