码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3648 Wedding

时间:2014-11-30 23:23:44      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

每对夫妻恰有一人坐在新娘对面,两个关系不正常的人不能都在新娘对面

问,是否有解

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 100;
struct twosat
{  
    int n,c;  
    vector<int> g[MAXN<<1];  
    bool mark[MAXN<<1];  
    int s[MAXN<<1];  
    
	bool dfs(int x) 
	{  
		int i;
        if (mark[x^1]) 
			return 0;  
        if (mark[x]) 
			return 1;  
        mark[x] = 1;  
        s[c++] = x;  
        for (i = 0; i < g[x].size(); i++)  
            if (!dfs(g[x][i])) 
				return 0;  
        return 1;  
    }  
    
    void init(int n) 
	{  
		int i,t=n<<1;
        this->n = n;  
        for (i = 0; i < t; i++)   
            g[i].clear();  
        memset(mark, 0, sizeof(mark));  
    }  
  
    void add_clause(int x,int y) 
	{  
        g[x].push_back(y^1);  
        g[y].push_back(x^1);
    }  
  
    bool solve() 
	{  
		int i,t=n<<1;
        for (i = 0; i < t; i += 2)   
            if (!mark[i] && !mark[i + 1]) 
			{  
                c = 0;  
                if (!dfs(i)) 
				{  
                    while (c > 0) 
						mark[s[--c]] =0;   
                    if (!dfs(i + 1)) 
						return 0;  
                }  
            }
        return 1;  
    }  
}ender;
int main()
{
	bool flag;
	int i,n,m,a,b,x,y;
	char t1,t2;
	while(cin>>n>>m)
	{
		if(n==0&&m==0)
			break;
		ender.init(n);
		ender.g[0].push_back(1);
		while(m--)
		{
			scanf("%d%c%d%c",&a,&t1,&b,&t2);
			if(t1=='w')
				x=0;
			else
				x=1;
			if(t2=='w')
				y=0;
			else
				y=1;
			a=a*2+x;
			b=b*2+y;
			ender.add_clause(a,b);
		}
		if(ender.solve())
		{
			n<<=1;
			flag=0;
			for(i=2;i<n;i++)
				if(ender.mark[i]&&i!=1)
				{
					if(flag)
						cout<<" ";
					cout<<i/2;
					if(i&1)
						cout<<"w";
					else
						cout<<"h";
					flag=1;
				}
		}
		else
			cout<<"bad luck";
		cout<<endl;
	}
	return 0;
}

Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8519   Accepted: 2571   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

Source


poj 3648 Wedding

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://blog.csdn.net/stl112514/article/details/41628879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!