题目:

http://poj.org/problem?id=3648

题意:

有n对夫妻参加婚礼,新郎新娘分别坐在桌子的两侧,要求新娘看到的那一侧(也就是新郎所在一侧)不能有一对夫妻,而且不能有通奸关系,然后输出新娘所在这一侧的人(没有关系限制)

思路:

可以发现新郎这一侧就是基础的2-sat问题,于是我们求新郎这一侧的人,连边建图,很简单,求出结果后输出跟新娘染成一个眼神的人。注意一点,因为我们求得这个解必定要包含新郎,但是求解过程中也许这个解包含了新娘(此时就不包含新郎了),怎么办呢?我们从新娘到新郎连一条边,那么如果选择了新娘就必须选择新郎,但这是矛盾的,所以这样做以后选出的解必定不包含新娘,很巧妙。

复杂度O(m),留作模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 110;
struct edge
{
    int to, next;
}g[N*N*2], g1[N*N*2];
int cnt, head[N], cnt1, head1[N];
int dfn[N], low[N], scc[N], st[N], top, num, idx;
int indeg[N], color[N], pos[N];
bool vis[N];
int n, m;
void add_edge(int v, int u)
{
    g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++;
}
void add_edge1(int v, int u)
{
    g1[cnt1].to = u, g1[cnt1].next = head1[v], head1[v] = cnt1++;
}
void init()
{
    memset(head, -1, sizeof head);
    memset(dfn, -1, sizeof dfn);
    memset(vis, 0, sizeof vis);
    top = num = idx = cnt = 0;
}
void tarjan(int v)
{
    dfn[v] = low[v] = ++idx;
    vis[v] = true, st[top++] = v;
    int u;
    for(int i = head[v]; i != -1; i = g[i].next)
    {
        u = g[i].to;
        if(dfn[u] == -1)
        {
            tarjan(u);
            low[v] = min(low[v], low[u]);
        }
        else if(vis[u])low[v] = min(low[v], dfn[u]);
    }
    if(dfn[v] == low[v])
    {
        num++;
        do
        {
            u = st[--top];
            vis[u] = false;
            scc[u] = num;
        }while(u != v);
    }
}
void toposort()
{
    queue<int> que;
    for(int i = 1; i <= num; i++)
        if(indeg[i] == 0) que.push(i);
    while(! que.empty())
    {
        int v = que.front(); que.pop();
        if(color[v] == 0) color[v] = 1, color[pos[v]] = 2;
        for(int i = head1[v]; i != -1; i = g1[i].next)
        {
            int u = g1[i].to;
            if(--indeg[u] == 0) que.push(u);
        }
    }
}
bool solve()
{
    for(int i = 0; i < 2*n; i++) //强连通缩点
        if(dfn[i] == -1) tarjan(i);
    for(int i = 0; i < 2*n; i += 2)
    {
        if(scc[i] == scc[i+1]) return false; //夫妻在同一个强连通分量内,无解
        pos[scc[i]] = scc[i+1], pos[scc[i+1]] = scc[i]; //记录对方在哪个强连通分量内
    }
    cnt1 = 0;
    memset(head1, -1, sizeof head1);
    memset(indeg, 0, sizeof indeg);
    memset(color, 0, sizeof color);
    for(int i = 0; i < 2*n; i++)
        for(int j = head[i]; j != -1; j = g[j].next)
            if(scc[i] != scc[g[j].to]) //反向建图,以便自底向上拓扑排序
                add_edge1(scc[g[j].to], scc[i]), indeg[scc[i]]++;
    toposort();
    return true;
}
int main()
{
    while(scanf("%d%d", &n, &m), n || m)
    {
        init();
        int a, b, v, u;
        char ch1, ch2;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%c%d%c", &a, &ch1, &b, &ch2);
            if(ch1 == 'h') v = a*2 + 1;
            else v = a*2;
            if(ch2 == 'h') u = b*2 + 1;
            else u = b*2;
            add_edge(v, u^1), add_edge(u, v^1);
        }
        add_edge(0, 1);
        if(solve())
            for(int i = 2; i < 2*n; i += 2)
            {
                if(color[scc[i]] == color[scc[0]]) printf("%dw", i/2);
                else printf("%dh", i/2);
                printf("%c", i == 2*(n-1) ? '\n' : ' ');
            }
        else printf("bad luck\n");
    }
    return 0;
}