(beginer)DFS (2-SAT) UVA 11294 Wedding

本文探讨如何运用2-SAT问题解决婚礼座次安排难题,确保新娘不看到坐在同一侧的配偶和有婚外情的人,避免不幸。通过解析案例输入和输出,了解如何使用2-SAT算法解决实际问题。

Problem E: Wedding

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

Sample Input

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

Possible Output for Sample Input

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

题意:最多30组人要参加婚礼,然后要分开坐在桌子两边,新郎和新娘在桌子两侧,新娘只能看到桌子对面的人,然后他不能看到妻子和丈夫坐在同一侧,有些人关系也有问题,也不能看到他们坐在同一侧。要怎么安排座位呢?
思路:2-SAT问题,对于一组中的h,要么h坐在对面,要么w坐在对面,把两个作为两个点,对于第i组:2*i表示w坐在对面,2*i+1表示h坐在对面。然后要注意新郎可能也有问题,所以一开始dfs的时候应该先dfs(1),如果直接就不行,就是不行了,然后再从2开始dfs就行了。最后输出的时候要反过来输出:如果mark[2*i] =true ,输出h ,如果不是就输出w。最后说一下输入的时候用scanf("%d%c")因为可能会输入3h3w 没有空格。。。。人家说的

代码:
#include<iostream>
#include<cstring>
#include<string.h>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 1200;
vector<int> G[maxn];
bool mark[maxn];
int S[maxn] , c;
char buffer;
int n , m;

void init()
{
	for (int i = 0 ; i < maxn ; ++i) G[i].clear();
	memset(mark,0,sizeof(mark));
}

void add(int x,int xval,int y,int yval)
{
	x = 2*x+xval;
	y = 2*y+yval;
	G[x].push_back(y^1);
	G[y].push_back(x^1);
}

bool dfs(int x)
{
	if (mark[x^1]) return false;
	if (mark[x]) return true;
	mark[x] = true;
	S[c++] = x;
	for (int i = 0 ; i < G[x].size() ; ++i)
		if (!dfs(G[x][i])) return false; 
	return true;
}

bool Ok()
{
	if (!dfs(1)) return false; 
	for (int i = 2 ; i < 2*n ; i+=2)
	{
		c = 0;
		if (!mark[i] && !mark[i+1])
			if (!dfs(i)) 
			{
				while (c) { mark[S[--c]] = false; }
				if (!dfs(i+1)) return false;
			}
	}
	return true;
}

void input()
{
	while (m--)
	{
		int x , y , xval = 1 , yval = 1;
		scanf("%d%c",&x,&buffer);
		if (buffer=='w') xval = 0;
		scanf("%d%c",&y,&buffer);
		if (buffer=='w') yval = 0;
		add(x,xval,y,yval);
	}
}

void solve()
{
	if (Ok()) {
		bool first = true;
		for (int i = 1 ; i < n ; ++i) 
		{
			if (first) first = false;
			else printf(" ");
			if (mark[2*i]) printf("%dh",i);
			else printf("%dw",i);
		}
	} else { printf("bad luck"); }
	cout << endl;
}

int main()
{
	while (scanf("%d%d",&n,&m),n+m)
	{
		init();
		input();
		solve();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值