poj3648 2-SAT进阶 记录点拓扑

Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10556   Accepted: 3220   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
题意:题意:一对新婚的夫妇邀请(n-1)对夫妇来参加自己的宴会,这对新人以及这些受邀请的夫妇都坐在长桌子的两边,新娘和新郎分别坐在桌子的两侧,新娘不希望看到她邀请来的那些夫妇之中有妻子和丈夫坐在同一边的情况(即妻子和丈夫要分作桌子的两边),在这n对夫妇中有一些男女存在着暧昧的关系,所以新娘也不希望看到有暧昧关系的人坐在她对面的那一侧.求解是否存在一种满足新娘要求的座位分配方案,如果存在的话,那么就输出这方案,否则输出"bad luck".
这个问题本身就有矛盾点了,即男性还是女性坐在女0对面,所以不用拆点。
另外注意加边只要加两条单向边,因为只关注女0的对面有没有奸夫淫妇,所以就是一旦有一个奸夫淫妇中的一个在新娘对面,那么另外一个的配偶必然也会在其对面,所以建边两个,注意没有同性恋有暧昧关系也是一个非常重要的条件

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N=1005;
vector<int>v[N];
vector<int>g[N];
bool instack[N];
int q[N],low[N],dfn[N],bl[N],con[N],col[N],in[N];
int l,scnt,cnt,n,m;
void init(){
   memset(dfn,0,sizeof(dfn));
   memset(in,0,sizeof(in));
   memset(col,0,sizeof(col));
   for(int i=0;i<=2*n;++i) v[i].clear(),g[i].clear();
   l=scnt=cnt=0;
}
inline void add(int a,int b){v[a].push_back(b);}
void Tarjan(int u){
   instack[u]=1;
   low[u]=dfn[u]=++cnt;
   q[l++]=u;
   for(int i=0;i<(int)v[u].size();++i) {
    int x=v[u][i];
    if(!dfn[x]) {
        Tarjan(x);
        low[u]=min(low[u],low[x]);
    }
    else if(instack[x]&&dfn[x]<low[u]) low[u]=dfn[x];
   }
   if(low[u]==dfn[u]){
    int  t;++scnt;
    do{
        t=q[--l];
        bl[t]=scnt;
        instack[t]=0;
    }while(t!=u);
   }
}
void rebuild(){
    for(int i=0;i<2*n;++i) for(int j=0;j<(int)v[i].size();++j){
        int a=bl[i],b=bl[v[i][j]];
        if(a!=b) {
            ++in[a];
            g[b].push_back(a);
        }
    }
}
void topsort(){
    queue<int>Q;
    for(int i=1;i<=scnt;++i) if(!in[i]) Q.push(i);
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        if(!col[u]) {
            col[u]=1;
            col[con[u]]=2;
        }
        for(int i=0;i<(int)g[u].size();++i) {
            int x=g[u][i];
            --in[x];
            if(!in[x]) Q.push(x);
        }
    }
}
void solve(){
    for(int i=0;i<2*n;++i) if(!dfn[i]) Tarjan(i);
    for(int i=0;i<2*n;i+=2) {if(bl[i]==bl[i+1]) {puts("bad luck");return;}
    int a=bl[i],b=bl[i+1];
    con[a]=b;con[b]=a;}
    rebuild();
    topsort();
    for(int i=2;i<2*n;i+=2){
        if(i!=2) printf(" ");
        if(col[bl[i]]==col[bl[0]]) printf("%dw",i/2);
        else printf("%dh",i/2);
    }
    puts("");
}
int main(){
    while(scanf("%d%d",&n,&m),n+m){
        init();
        int a,b;
        char c,d;
        while(m--){
            scanf("%d%c %d%c",&a,&c,&b,&d);
            if(c=='h') a=2*a+1;else a=2*a;
            if(d=='h') b=2*b+1;else b=2*b;
            add(a,b^1);
            add(b,a^1);
        }
        add(0,1);
        solve();
    }
}

 

posted @ 2017-08-07 15:25  Billyshuai  阅读(139)  评论(0编辑  收藏  举报