POJ 3648 Wedding 2-SAT 输出任意一个2-sat解

73 篇文章 21 订阅
15 篇文章 0 订阅

题目描述:

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

 

题解:

用的是这篇论文

赵爽的《2-SAT解法浅析》

题目比较恶心,男跟男都有通奸现象,然后需要注意的是新郎坐在新娘的对面。 有通奸关系的不能坐在同一边,要输出的是新娘这一边的人。

为了让丈夫一定坐在新娘对面,就连接新娘到新郎那条边。

 

代码:

/*
 * @author ipqhjjybj
 * @date 20130904
 */
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N=1111;
const int M=N*N;
int dfn[N],low[N],sig,tcc,sta[N],sa,id[N];
int head[N],cnt;
int n,m;
struct node{
    int to,next,from;
}Edge[M];
void addedge(int u,int v){
    Edge[cnt].to=v;
    Edge[cnt].from=u;
    Edge[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int u){
    int tmp=dfn[u]=low[u]=sig++;
    sta[sa++]=u;
    for(int q=head[u];q!=-1;q=Edge[q].next){
        int v=Edge[q].to;
        if(dfn[v]==-1) dfs(v);
        tmp=min(tmp,low[v]);
    }
    if(tmp<low[u]){
        low[u]=tmp;
        return;
    }int t;
    do{
        id[t=sta[--sa]]=tcc;
        low[t]=n<<1;
    }while(t!=u);
    tcc++;
}
int fc[N],c[N];
#define R 1
#define B -1
int cnt2,head2[N],in[N],ans[N];
struct node2{
    int to,next;
}Edge2[M];
void addEdge2(int u,int v){
    Edge2[cnt2].to=v;
    Edge2[cnt2].next=head2[u];
    head2[u]=cnt2++;
}
void topSort(){
    queue<int> Q;
    for(int i=0;i<tcc;i++){
        if(in[i]==0)
            Q.push(i);
    }
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        if(!c[u]){
            c[u]=R;c[fc[u]]=B;
        }
        for(int q=head2[u];q!=-1;q=Edge2[q].next){
            int v=Edge2[q].to;
            in[v]--;
            if(in[v]==0)
                Q.push(v);
        }
    }
    memset(ans,0,sizeof(ans));
    for(int i=0;i<n;i++){
        if(c[id[i]]==R){
            ans[i]=R;
        }
    }
}
bool solve(){
    memset(dfn,-1,sizeof(dfn));
    sig=tcc=sa=0;
    for(int i=0;i<(n<<1);i++){
        if(dfn[i]==-1){
            dfs(i);
        }
    }
    memset(c,0,sizeof(c));
    memset(fc,0,sizeof(c));
    for(int i=0;i<n;i++){
        if(id[i]==id[i+n])
            return false;
        fc[id[i]]=id[i+n];
        fc[id[i+n]]=id[i];
    }
    cnt2=0;
    memset(head2,-1,sizeof(head2));
    memset(in,0,sizeof(in));
    for(int i=0;i<cnt;i++){
        if(id[Edge[i].to]!=id[Edge[i].from]){
            addEdge2(id[Edge[i].to],id[Edge[i].from]); //反向加边
            in[id[Edge[i].from]]++;
        }
    }
    topSort();
    return true;
}
int main(){
   char str1,str2;
   int l,r;
   while(scanf("%d %d",&n,&m)&&(n||m)){
        cnt=0;
        memset(head,-1,sizeof(head));
        while (m--)
  {
   scanf("%d%c %d%c",&l,&str1,&r,&str2);
   //开始构图
   if (str1 == 'h' && str2 == 'h')
   {
    addedge(r+n,l);
    addedge(l+n,r);
   }
   else if (str1 == 'h' && str2 == 'w')
   {
    addedge(l+n,r+n);
    addedge(r,l);
   }
   else if (str1 == 'w' && str2 == 'h')
   {
    addedge(l,r);
    addedge(r+n,l+n);
   }
   else if (str1 == 'w' && str2 == 'w')
   {
    addedge(l,r+n);
    addedge(r,l+n);
   }
  }
  addedge(0,0+n);  //增加新娘到新郎的边
        if(!solve()){
            printf("bad luck\n");
        }else{
            for(int i=1;i<n;i++){
                if(ans[i]){
                   printf("%dh%c",i,i==n-1?'\n':' ');
                }else{
                   printf("%dw%c",i,i==n-1?'\n':' ');
                }
            }
        }
   }
   return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值