0
点赞
收藏
分享

微信扫一扫

poj3648 Wedding

东林梁 2022-08-08 阅读 40


​​http://www.elijahqi.win/archives/3077​​​
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

Waterloo Local Contest, 2007.9.29
将每对cp拆开 wife是下标是偶数 husband下标是奇数
然后2-sat连接限制条件的时候就 将是否坐在新郎那一边作为限制条件 满足题目中说不能坐在一起的就限制一下使得他们不能同时坐在新郎的那一边即可 注意本题中新娘也可能xx 新郎也可能xx但是新娘和另一个xx的人坐在一排不会有问题 所以这种情况需要特判断 同时也需要限制新娘在合法的位置新郎在合法的位置 然后就是常见的输出方案的那套理论了

#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=200;
const int M=1e5;
char s[2],s1[2];bool stackf[N];
int n,m,x,xx,num,h[N],h1[N],dfn[N],low[N],q[N],top,ss,cnt,id[N][2],b[N],op[N],in[N],col[N];
struct node{
int y,next,x;
}data[M],data1[M];
inline void insert1(int x,int y){
data[++num].y=y;data[num].next=h[x];h[x]=num;data[num].x=x;
}
inline void tarjan(int x){
dfn[x]=low[x]=++num;stackf[x]=1;q[++top]=x;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if(!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);else if (stackf[y]) low[x]=min(low[x],dfn[y]);
}
if (dfn[x]==low[x]){
int y;++ss;
do{y=q[top--];stackf[y]=0;b[y]=ss;}while(y!=x);
}
}
inline void insert2(int x,int y){
data1[++num].y=y;data1[num].next=h1[x];h1[x]=num;
}
int main(){
//freopen("poj3648.in","r",stdin);
while(1){
scanf("%d%d",&n,&m);if (!n&&!m) return 0;cnt=-1;
for (int i=0;i<n;++i) id[i<<1][0]=++cnt,id[i<<1][1]=++cnt,
id[i<<1|1][0]=++cnt,id[i<<1|1][1]=++cnt;// 0 the sides of groom
num=0;memset(h,0,sizeof(h));//even bride odd groom
for (int i=1;i<n;++i){
insert1(id[i<<1][0],id[i<<1|1][1]);insert1(id[i<<1|1][0],id[i<<1][1]);
insert1(id[i<<1][1],id[i<<1|1][0]);insert1(id[i<<1|1][1],id[i<<1][0]);
}
for (int i=1;i<=m;++i){static int t1,t2;
scanf("%d%s",&x,s);scanf("%d%s",&xx,s1);
if(s[0]=='h') t1=x<<1|1;else t1=x<<1;
if(s1[0]=='h') t2=xx<<1|1;else t2=xx<<1;
insert1(id[t1][0],id[t2][1]);insert1(id[t2][0],id[t1][1]);
}insert1(id[0][0],id[0][1]);insert1(id[1][1],id[1][0]);
//for (int i=1;i<=num;++i) printf("%d %d\n",data[i].x,data[i].y);
num=0;memset(dfn,0,sizeof(dfn));ss=0;bool flag=0;
for (int i=0;i<=cnt;++i) if(!dfn[i]) tarjan(i);
for (int i=0;i<n<<1;++i){
if (b[id[i][0]]==b[id[i][1]]) {puts("bad luck");flag=1;break;}
op[b[id[i][0]]]=b[id[i][1]];op[b[id[i][1]]]=b[id[i][0]];
}if (flag) continue;num=0;memset(h1,0,sizeof(h1));memset(in,0,sizeof(in));
for (int i=0;i<cnt;++i){
for (int j=h[i];j;j=data[j].next){
int y=data[j].y;if (b[i]==b[y]) continue;
insert2(b[y],b[i]);++in[b[i]];
}
}queue<int>q;memset(col,-1,sizeof(col));
for (int i=1;i<=ss;++i) if (!in[i]) q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
if(col[x]==-1){col[x]=1;col[op[x]]=0;}
for (int i=h1[x];i;i=data1[i].next){
int y=data1[i].y;if(!--in[y]) q.push(y);
}
}
for (int i=2;i<n<<1;++i){
if (col[b[id[i][1]]]==1){
printf("%d",i>>1);if (i&1) printf("h ");else printf("w ");
}
}puts("");
}
return 0;
}


举报

相关推荐

0 条评论