Wedding(2 - sat)

Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7920   Accepted: 2394   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(<= 30)对夫妇 和 M 对通奸关系。现在这些人要和一堆新婚夫妇在饭桌前吃饭,饭桌面对着共有两边。夫妻必须面对面做,新娘能望见对面的所有人,新娘不能同时看见一对通奸关系的人。现要安排座位,是否能按要求分配座位。

 

       思路:

       2 - sat。新娘新郎的位置要判矛盾,故要增加两条边。

 

       AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

const int NMAX = 65 * 2;

int n, m;
vector<int> G[NMAX];
int res[NMAX];

int pre[NMAX], low[NMAX], cmp[NMAX];
int dfs_clock, scc_cnt;
stack<int> s;

void dfs(int u) {
        pre[u] = low[u] = ++dfs_clock;
        s.push(u);

        for (int i = 0; i < G[u].size(); ++i) {
                int v = G[u][i];
                if (!pre[v]) {
                        dfs(v);
                        low[u] = min(low[u], low[v]);
                } else if (!cmp[v]) {
                        low[u] = min(low[u], pre[v]);
                }
        }

        if (pre[u] == low[u]) {
                ++scc_cnt;
                for ( ;;) {
                        int x = s.top(); s.pop();
                        cmp[x] = scc_cnt;
                        if (x == u) break;
                }
        }

}

void scc() {
        dfs_clock = scc_cnt = 0;
        memset(pre, 0, sizeof(pre));
        memset(cmp, 0, sizeof(cmp));

        for (int i = 0; i < 2 * n; ++i) {
                if (!pre[i]) dfs(i);
        }
}

int main() {

        while (~scanf("%d%d", &n, &m) && (n + m)) {
                n *= 2;

                for (int i = 0; i < 2 * n; ++i)
                        G[i].clear();

                for (int i = 0; i < n; i += 2) {
                        G[i].push_back(i + 1 + n);
                        G[i + n].push_back(i + 1);
                        G[i + 1].push_back(i + n);
                        G[i + 1 + n].push_back(i);
                }

                G[n + 0].push_back(0);  //固定新娘只能在一边坐
                G[1].push_back(1 + n);  //固定新郎只能在对面坐

                while (m--) {
                        int A, B;
                        char sex;
                        scanf("%d%c", &A, &sex);
                        A = A * 2 + (sex == 'h');
                        scanf("%d%c", &B, &sex);
                        B = B * 2 + (sex == 'h');
                        G[A + n].push_back(B);
                        G[B + n].push_back(A);
                }

                scc();

                int temp = 0;
                for (int i = 0; i < n; ++i) {
                        if (cmp[i] == cmp[i + n]) {
                                printf("bad luck\n");
                                temp = 1;
                                break;
                        }
                }

                if (!temp) {
                        int ans = 0;
                        for (int i = 2; i <= n; ++i)
                                if (cmp[i] < cmp[i + n]) res[ans++] = i;

                        for (int i = 0; i < ans; ++i) {
                                printf("%d", res[i] / 2);
                                res[i] % 2 ? printf("h") : printf("w");
                                i == ans - 1 ? printf("\n") : printf(" ");
                        }
                }

        }

        return 0;
}

 

你可能感兴趣的:(DI)