poj 3648 Wedding(2-SAT)

来源:互联网 发布:莫烦python教学网站 编辑:程序博客网 时间:2024/04/29 22:48

Language:
Wedding
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8753 Accepted: 2630 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 63h 7h5w 3w7h 6w8w 3w7h 3w2w 5h0 0

Sample Output

1h 2h 3w 4h 5h 6h 7h 8h 9h


情侣结婚 需要宴请宾客 总共有n对情侣 需要这n对情侣分别坐在两边 不能坐在一边

另外呢 还有m对暧昧关系的 这些保持暧昧关系的也需要坐在两边 

如果有可行的方案 输出与新娘同侧的人

对于第i对情侣  xi w的反为xi h 

对于第j对暧昧关系  xj与yj为假  从而有蕴含关系。。。

此外  还要记得 添加新娘到新郎的边

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 5010#define MAXM 2000100#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char ch;    int a = 0;    while((ch = getchar()) == ' ' | ch == '\n');    a += ch - '0';    while((ch = getchar()) != ' ' && ch != '\n')    {        a *= 10;        a += ch - '0';    }    return a;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}struct Edge{    int to,next;}edge[MAXM];int head[MAXN],tot;int low[MAXN],dfn[MAXN],stack[MAXN],belong[MAXN];int index,top;bool instack[MAXN];int scc;void addedge(int u,int v){    edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;}void Tarjan(int u){    int v;    low[u]=dfn[u]=++index;    stack[top++]=u;    instack[u]=1;    for(int i=head[u];i!=-1;i=edge[i].next)    {        v=edge[i].to;        if(!dfn[v])        {            Tarjan(v);            if(low[u]>low[v]) low[u]=low[v];        }        else if(instack[v]&&low[u]>dfn[v])            low[u]=dfn[v];    }    if(low[u]==dfn[u])    {        scc++;        do        {            v=stack[--top];            instack[v]=0;            belong[v]=scc;        }while(u!=v);    }}int solvable(int N){    MEM(dfn,0); MEM(instack,0);    index=scc=top=0;    for(int i=0;i<N;i++)        if(!dfn[i])            Tarjan(i);    for(int i=0;i<N;i+=2)    {        if(belong[i]==belong[i^1])        {            return 0;        }    }    return 1;}void init(){    tot=0;    MEM(head,-1);}int s[MAXN],t[MAXN],d[MAXN];queue<int> q1,q2;vector<vector<int> > dag;char color[MAXN];int indeg[MAXN];int cf[MAXN];void solve(int n){    dag.assign(scc+1,vector<int>());    MEM(indeg,0); MEM(color,0);    for(int u=0;u<n;u++)    {        for(int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].to;            if(belong[u]!=belong[v])            {                dag[belong[v]].push_back(belong[u]);                indeg[belong[u]]++;            }        }    }    for(int i=0;i<n;i+=2)    {        cf[belong[i]]=belong[i^1];        cf[belong[i^1]]=belong[i];    }    while(!q1.empty()) q1.pop();    while(!q2.empty()) q2.pop();    for(int i=1;i<=scc;i++)        if(indeg[i]==0)            q1.push(i);    while(!q1.empty())    {        int u=q1.front();        q1.pop();        if(color[u]==0)        {            color[u]='R';            color[cf[u]]='B';        }        int sz=dag[u].size();        for(int i=0;i<sz;i++)        {            indeg[dag[u][i]]--;            if(indeg[dag[u][i]]==0)                q1.push(dag[u][i]);        }    }}int getnum(char ch[]){    int ans=0;    int i=0;    while(ch[i]>='0'&&ch[i]<='9')    {        ans*=10;        ans+=ch[i]-'0';        i++;    }    if(ch[i]=='w') return 2*ans;    else return 2*ans+1;}int main(){//    fread;    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n==0&&m==0) break;        init();        while(m--)        {            char ch1[10],ch2[10];            scanf("%s%s",ch1,ch2);            int u=getnum(ch1);            int v=getnum(ch2);            addedge(u^1,v);            addedge(v^1,u);        }        addedge(1,0);        int flag=solvable(2*n);        if(!flag)        {            puts("bad luck");            continue;        }        solve(2*n);        for(int i=1;i<n;i++)        {            if(color[belong[2*i]]=='R') printf("%dw",i);            else printf("%dh",i);            if(i<n-1) printf(" ");            else printf("\n");        }    }    return 0;}





0 0
原创粉丝点击