## poj 3648 Wedding

2019年11月08日 算法 ⁄ 共 2408字 ⁄ 字号 评论关闭

```#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));
}

{
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.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;
}
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<<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