【 POJ - 3648】Wedding(2-SAT之构造解)

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
难度不大,但是莫名其妙的wa。。。从昨天一直wa到现在,,,反正现在算是过了把
代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 5005
#define maxx 500005
#define ll long long
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
vector<int> g[maxn],_g[maxn];
int dfn[maxn],low[maxn];
bool inS[maxn];
int tot,_count,id[maxn],opp[maxn],ind[maxn];
int color[maxn];
queue<int>que;
int st[maxn],cnt;
void init()
{
    tot=_count=0;
    for(int i=0;i<2*n;i++)
        g[i].clear(),_g[i].clear();
    memset(inS,0,sizeof(inS));
    memset(dfn,0,sizeof(dfn));
    memset(ind,0,sizeof(ind));
    memset(color,0,sizeof(color));
    while(que.size())que.pop();
}
void tarjan(int u)
{
    dfn[u]=low[u]=++_count;
    inS[u]=true;st[++cnt]=u;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(inS[v])low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        ++tot;int now;
        do
        {
            now=st[cnt--];
            inS[now]=false;
            id[now]=tot;
        }while(now!=u);
    }
}
void topo()
{
    for(int i=1;i<=tot;i++)
        if(ind[i]==0)que.push(i);
    while(que.size())
    {
        int u=que.front();que.pop();
        if(!color[u])
        {
            color[u]=1;
            color[opp[u]]=2;
        }
        for(int i=0;i<_g[u].size();i++)
        {
            int v=_g[u][i];
            --ind[v];
            if(ind[v]==0)
                que.push(v);
        }
    }

}
int main()
{
    char c1,c2;
    int x,y;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0&&m==0)break;
        init();
        while(m--)
        {
            scanf("%d%c%d%c",&x,&c1,&y,&c2);
            x<<=1;y<<=1;
            if(c1=='h')x++;
            if(c2=='h')y++;
            if(x!=(y^1))
            {
                g[x].push_back(y^1);
                g[y].push_back(x^1);
            }
        }
        g[0].push_back(1);
        for(int i=0;i<2*n;i++)
            if(!dfn[i])tarjan(i);
        bool sign=true;
        for(int i=0;i<n;i++)
        {
            if(id[i*2]==id[i*2+1])
            {
                sign=false;
                break;
            }
            else
            {
                opp[id[2*i]]=id[2*i+1];
                opp[id[2*i+1]]=id[2*i];
            }
        }
        if(!sign)
        {
            printf("bad luck\n");
            continue;
        }
        for(int u=0;u<2*n;u++)
        {
            for(int i=0;i<g[u].size();i++)
            {
                int v=g[u][i];
                if(id[u]!=id[v])
                {
                    _g[id[v]].push_back(id[u]);
                    ind[id[u]]++;
                }
            }
        }
        topo();
        for(int i = 2; i < 2 * n; i += 2)
            if(color[id[i]] == color[id[0]]) printf("%dw ", i / 2);
            else printf("%dh ", i / 2);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(2-SAT)