Wedding(POJ 3648)---2-SAT模板题输出任意一组解(tarjan求解)

题目链接

题目描述

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.

输入格式

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.

输出格式

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”.

输入样例

10 6
3h 7h
5w 3w
7h 6w
8w 3w
7h 3w
2w 5h
0 0

输出样例

1h 2h 3w 4h 5h 6h 7h 8h 9h

分析

题目大意是有1对夫妻结婚,有n-1对夫妻前去参加婚礼,里面一共有m对通奸关系。现在有一张长桌,新娘新郎各自坐在一侧,要求剩下的人里面分别坐在长桌两侧,且满足以下两个条件:

  • 条件①:夫妻不能坐一排
  • 条件②:有通奸关系的不能同时坐在新娘对面。

如果没有解输出“bad luck”,否则输出任意一组解。
对于这道2-SAT模板题,因为要保证通奸关系不能同时坐在新娘对面,所以我们要先确定新娘的位置。假设新娘坐在左侧,我们确定命题为:是否坐在左侧?对于某个人i,我们用i表示i坐在左侧,用i+n表示i不坐在左侧即坐在右侧,所以有以下边:

对于新娘0和新郎1,有:

  • 新娘一定在左边:<2*n,0>
  • 新郎一定在右边:<2*n+1,1>

对于是第i对夫妻的妻子2i和丈夫2i+1,有:

  • 妻子在左边,丈夫在右边:<2i,2i+1+2*n>
  • 丈夫在左边,妻子在右边:<2i+1,2i+2*n>
  • 妻子在右边,丈夫在左边:<2i+2n,2*i+1>
  • 丈夫在右边,妻子在左边:<2i+1+2n,2*i>

对于有通奸关系的a和b,有:

  • 如果a和b中没有新郎新娘,则有:a在右边,b必在左边<a+2n,b>,b在右边,a必在左边<b,a+2n>。
  • 如果a和b中有新郎,则不是新郎的那一方必在右边,如b不是:<b+2*n,b>。

按照上述原则进行建图,至于为什么和新娘有通奸关系不用建边,那是因为不管是谁和新娘通奸,坐在左右两侧均不影响当前条件。
建完图后,用tarjan算法求强连通分量,对于每一个人i,如果它所属的强连通分量scc[i]满足scc[i]<scc[i+2*n],即选他在左边比不选他在左边的强连通分量编号小,就把他作为可行解的一部分。至于这样做的原理,那是因为按照tarjan算法求得题中存在可行解后,我们需要对缩点后的图求得其反图GT (对于这点不理解的可以参考一下这篇博客传送门按照反图的拓扑序来进行判断,先确定序小的是因为不会存在一个序大的点推翻它。而对原图tarjan算法求得的强连通分量,恰好其编号是满足的一个拓扑逆序,以下是源码。

源程序

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <stack>
#define MAXN 35*40
#define MAXM MAXN*MAXN
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
	int v,next;
	Edge(){}
	Edge(int v,int next):v(v),next(next){}
}edge[MAXM];
int EdgeCount,head[MAXN];
int n,m,block_cnt,scc_cnt; 
int dfn[MAXN],low[MAXN],scc[MAXN];
bool vis[MAXN];
stack<int> s;
void addEdge(int u,int v)
{
	edge[++EdgeCount]=Edge(v,head[u]);
	head[u]=EdgeCount;	
} 
void init()
{
	memset(head,0,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(scc,0,sizeof(scc));
	memset(vis,false,sizeof(vis));
	EdgeCount=block_cnt=scc_cnt=0;
}
void tarjan(int u)
{
	dfn[u]=low[u]=++block_cnt;	//时间戳
	vis[u]=true; 
	s.push(u);
	for(int i=head[u];i;i=edge[i].next){
		int v=edge[i].v;
		if(!dfn[v]){	//还没访问过 
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(vis[v])	//访问过且在栈中
			low[u]=min(low[u],dfn[v]); 
	}
	if(dfn[u]==low[u]){	//满足强连通分量 
		scc_cnt++;	
		while(1){
			int tmp=s.top();s.pop();
			scc[tmp]=scc_cnt;	//标记所属强连通分量 
			vis[tmp]=false;		//标记出栈
			if(tmp==u)break; 
		}
	}
}
bool TwoSat()
{
	for(int i=0;i<4*n;i++)
		if(!dfn[i])
			tarjan(i);
	//for(int i=0;i<2*n;i++)
	//	printf("%d: %d %d\n",i,scc[i],scc[i+2*n]);
	for(int i=0;i<2*n;i++)
		if(scc[i]==scc[i+2*n])
			return false;
	return true;
}
int main()
{
	while(scanf("%d%d",&n,&m)&&(n+m)){
		init();
		addEdge(2*n,0);		//新娘一定在左边 
		addEdge(1,2*n+1);	//新郎一定在右边 
		for(int i=1;i<n;i++){
			int a=i*2;		//妻子 
			int b=i*2+1;	//丈夫 
			addEdge(a,b+2*n);	//妻子右边,丈夫左边
			addEdge(b,a+2*n);	//丈夫右边,妻子左边	
			addEdge(a+2*n,b);	//妻子左边,丈夫右边
			addEdge(b+2*n,a);	//丈夫左边,妻子右边 
		} 
		for(int i=1;i<=m;i++){
			int a,b;	//有奸情的人
			char xb1,xb2;	//性别
			scanf("%d%c %d%c",&a,&xb1,&b,&xb2);
			if(xb1=='h')a=2*a+1;
			else a=2*a;
			if(xb2=='h')b=2*b+1;
			else b=2*b; 
			if(a>1&&b>1){	//都不与新郎新娘有奸情 
				addEdge(a+2*n,b);	//a在右边,b在左边
				addEdge(b+2*n,a);	//b在右边,a在左边 
			}
			else if(a==1||b==1){	//与新郎有奸情	 
				if(a==1)
					addEdge(b+2*n,b);	//b一定在左边 
				else
					addEdge(a+2*n,a);	//a一定在左边 
			} 
		}
		if(!TwoSat())
			printf("bad luck\n");
		else{
			int flag=false;
			for(int i=2;i<2*n;i++){
				if(scc[i]<scc[i+2*n]){	//选所属强连通分量编号小的那个 
					if(flag)
						printf(" %d%c",i/2,i%2==0?'w':'h');
					else{
						flag=true;
						printf("%d%c",i/2,i%2==0?'w':'h');
					}
				}
			} 
			printf("\n");
		} 
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值