POJ - 3648 Wedding

版权声明:选经典题目,写精品文章. https://blog.csdn.net/nka_kun/article/details/83036274

Wedding
Time Limit: 1000MS Memory Limit: 65536K
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

题意:好可怕的题意。。
思路:2-SAT 模型,夫妻两个不能同时坐在新娘对面,然后建边的时候注意新娘新郎可能也得建边,所以应该把新娘同所有人建边,这样才能保证跑出来的是正确的.
模型一:两者(A,B)不能同时取
  那么选择了A就只能选择B’,选择了B就只能选择A’
  连边A→B’,B→A’
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 200000
#define MAXM 200000
#define mem(a,b) memset(a,b,sizeof(a))

int n,m;
struct node
{
    int v;
    node *next;
};

node edge[MAXM*2];
node *cnt=&edge[0];
node *adj[MAXN];
node edge2[MAXM*2];
node *cnt2=&edge2[0];
node *adj2[MAXN];
int dfn[MAXN],low[MAXN],dcnt;
int stack[MAXN],top;
int Belong[MAXN],Num[MAXN],opp[MAXN],scc;
int In[MAXN],q[MAXN],col[MAXN];
bool Instack[MAXN],ans[MAXN];

inline void Get_int(int &Ret)
{
    char ch;
    bool flag=false;
    for(;ch=getchar(),ch<'0'||ch>'9';)
        if(ch=='-')
            flag=true;
    for(Ret=ch-'0';ch=getchar(),ch>='0'&&ch<='9';Ret=Ret*10+ch-'0');
    flag&&(Ret=-Ret);
}

inline int Get(int x)
{
    if(x%2)
        return x+1;
    return x-1;
}


inline void Addedge(int u,int v)
{
    node *p=++cnt;
    p->v=v;
    p->next=adj[u];
    adj[u]=p;
}

inline void Addedge2(int u,int v)
{
    node *p=++cnt2;
    p->v=v;
    p->next=adj2[u];
    adj2[u]=p;
}

void Read()
{
	n+= n; 
    int i,j,k;
    for(int i = 0;i<= n+n;i++)
    {
    	dfn[i]=low[i]= stack[i]=Belong[i] = Num[i] =opp[i] = In[i]= q[i]= col[i]=Instack[i] = ans[i] = 0;
    	adj[i] = adj2[i] = NULL;
    }
    cnt=&edge[0];
    cnt2=&edge2[0];
    for(i=1;i<=m;i++)
    {
    	int x,y;
    	char c,d;
        scanf("%d%c %d%c",&x,&c,&y,&d);
        x++;
        y++;
        x*= 2;
        y*= 2;
        if(c == 'h') x--;// husband
        if(d == 'h') y--;
     //   cout<<x<<' '<<y<<endl;
        Addedge(x,Get(y));
        Addedge(y,Get(x));
    }
    for(int i = 3;i<= n;i++)
	{
		Addedge(2,Get(i));
        Addedge(i,Get(2));
	}
    return ;
}

void Tarjan(int u)
{
    int v;
    dfn[u]=low[u]=++dcnt;
    stack[++top]=u;
    Instack[u]=true;
    for(node *p=adj[u];p;p=p->next)
    {
        v=p->v;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(Instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scc++;
        do
        {
            v=stack[top];
            top--;
            Instack[v]=false;
            Belong[v]=scc;
            Num[scc]++;
        }while(v!=u);
    }
}

bool Work()
{
    int i;
    for(i=1;i<=n;i++)
        if(!dfn[i])
            Tarjan(i);
    for(i=1;i<=n;i+=2)
    {
        if(Belong[i]==Belong[i+1])
            return false;
        opp[Belong[i]]=Belong[i+1];
        opp[Belong[i+1]]=Belong[i];
    }
    int u,v;
    for(i=1;i<=n;i++)
        for(node *p=adj[i];p;p=p->next)
        {
            v=p->v;
            if(Belong[i]!=Belong[v])
            {
                Addedge2(Belong[v],Belong[i]);
                In[Belong[i]]++;
            }
        }
    int l=0,r=0;
    for(i=1;i<=scc;i++)
        if(!In[i])
        {
            q[r]=i;
            r++;
        }
    while(l<r)
    {
        u=q[l];
        l++;
        if(!col[u])
        {
            col[u]=1;
            col[opp[u]]=-1;
        }
        for(node *p=adj2[u];p;p=p->next)
        {
            v=p->v;
            In[v]--;
            if(!In[v])
            {
                q[r]=v;
                r++;
            }
        }
    }
    for(i=1;i<=n;i+=2)
        if(col[Belong[i]]==1)
            ans[i]=true;
    return true;
}


void Print()
{
    if(Work())
    {
        int i;
        for(i=3;i< n-1;i+= 2)
        {
        	printf("%d",(i+1)/2-1);
        	if(ans[i]) printf("w ");
        	else printf("h ");
        }
        printf("%d",n/2-1);
        if(ans[n-1]) printf("w\n");
        else printf("h\n");
    }
    else
        printf("bad luck\n");
}

int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		if(n == 0&&m == 0) break;
		dcnt = scc = top = 0;
		
		Read();
    	Print();
	}
	
    return 0;
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/83036274