poj 3648 Wedding

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

问,是否有解

#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 givesn, 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 ton - 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值