2-sat->poj 3648 Wedding

Wedding
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7406 Accepted: 2254 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
 
构图求解:
新娘编号0, 从n - 1 对夫妇中选择n - 1个不相容的坐在新娘的对面。加边add(0, n),新娘必不选。
 
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stack>
#include <queue>

using namespace std;

#define MAXN 200 + 10

struct Edge
{
	int u, v, next;
}edge[200 * MAXN], edgetwo[200 * MAXN];

int head[MAXN], e, etwo, cnt, ind, n, m;
int dfn[MAXN], low[MAXN], belong[MAXN], cf[MAXN], col[MAXN];
int indegree[MAXN], headtwo[MAXN];
bool ins[MAXN];

void add(int u, int v)
{
	edge[e].u = u;
	edge[e].v = v;
	edge[e].next = head[u];
	head[u] = e++;
}

void insert(int u, int v)
{
	edgetwo[etwo].v = v;
	edgetwo[etwo].next = headtwo[u];
	headtwo[u] = etwo++;
}

queue <int> q;
stack <int> s;

void init()
{
	e = etwo = ind = cnt = 0;
	
	while (!q.empty())
	{
		q.pop();
	}
	
	while (!s.empty())
	{
		s.pop();
	}
	
	memset(head, -1, sizeof(head));
	memset(headtwo, -1, sizeof(headtwo));
	memset(dfn, 0, sizeof(dfn));
	memset(low, 0, sizeof(low));
	memset(indegree, 0, sizeof(indegree));
	memset(cf, 0, sizeof(cf));
	memset(col, 0, sizeof(col));
	memset(belong, 0, sizeof(belong));
	memset(ins, false, sizeof(ins));
}

void tarjan(int u)
{
	dfn[u] = low[u] = ++ind;
	
	ins[u] = true;
	
	s.push(u);
	
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].v;
		
		if (!dfn[v])
		{
			tarjan(v);
			
			low[u] = min(low[u], low[v]);
		}
		else if (ins[v])
		{
			low[u] = min(low[u], dfn[v]);
		}
	}
	
	if (low[u] == dfn[u])
	{
		cnt++;
		
		int x;
		
		do
		{
			x = s.top();
			s.pop();
			ins[x] = false;
			belong[x] = cnt;
		}while (x != u);
	}
}

void torpu()
{
	for (int i = 1; i <= cnt; i++)
	{
		if (indegree[i] == 0)
		{
			q.push(i);
		}
	}
	
	while (!q.empty())
	{
		int cur = q.front();
		q.pop();
		
		if (col[cur] == 0)
		{
			col[cur] = 1;
			col[cf[cur]] = -1;
		}
		
		for (int i = headtwo[cur]; i != -1; i = edgetwo[i].next)
		{
			int v = edgetwo[i].v;
			
			if (--indegree[v] == 0)
			{
				q.push(v);
			}
		}
	}
}

void solve()
{
	for (int i = 0; i < 2 * n; i++)
	{
		if (!dfn[i])
		{
			tarjan(i);
		}
	}
	
	bool flag = true;
	
	for (int i = 0; i < n; i++)
	{
		if (belong[i] == belong[i + n])
		{
			flag = false;
		}
		cf[belong[i]] = belong[i + n];
		cf[belong[i + n]] = belong[i];
	}
	
	if (!flag)
	{
		cout << "bad luck" << endl;
	}
	else
	{
		for (int i = 0; i < e; i++)
		{
			if (belong[edge[i].u] != belong[edge[i].v])
			{
				insert(belong[edge[i].v], belong[edge[i].u]);
				indegree[belong[edge[i].u]]++;
			}
		}
		
		torpu();
		
		int num = 0;
		
		for (int i = 1; i < n; i++)
		{
			if (col[belong[i]] == 1)
			{
				cout << i << 'h';
			}
			else
			{
				cout << i << 'w';
			}
			
			if (i != n - 1)
			{
				cout << ' ';
			}
		}
		
		cout << endl;
	}
}

void input()
{
	int u, v;
	char ch1, ch2;
	
	while (cin >> n >> m, n + m)
	{
		init();
        
		for (int i = 0; i < m; i++)
		{
			cin >> u >> ch1 >> v >> ch2;
			
			if (v == 0 && ch2 == 'w')
			{
				continue;
			}
			
			if (ch1 == 'w' && ch2 == 'w')
			{
				add(u, v + n);
				add(v, u + n);
			}
			else if (ch1 == 'w' && ch2 == 'h')
			{
				add(u, v);
				add(v + n, u + n);
			}
			else if (ch1 == 'h' && ch2 == 'w')
			{
				add(v, u);
				add(u + n, v + n);
			}
			else
			{
				add(u + n, v);
				add(v + n, u);
			}
		}
		
		add(0, 0 + n);
		solve();
	}
}

int main()
{
	input();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值