poj3648Wedding【2-SAT】输出任意解

Total Submissions: 9574

    Accepted: 2908   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

Source

Waterloo Local Contest, 2007.9.29

题意:

一堆夫妇去参加一对新人的婚礼。人们坐在一个很长很长的桌子的两侧(面对面)。新郎新娘在桌子头面对面座。

新娘不希望看见她对面的一排有一对夫妇坐着(夫妇需要分开两排座)。

同时,一些人之间有通奸关系(通奸不分性别,而且新郎新娘也可能通奸!!!!),新娘也不希望有通奸关系的人同时坐在她对面的一排。

问你可否满足新娘的要求,可以的话,输出一种方案

做法:和hdu1814Peaceful Commission【2-SAT】输出最小解   一模一样,实在不理解为啥非得强连通分量缩点,反向拓扑排序……

这个题套入2-sat的真假有点绕,我们假设新娘这侧选h是“真”(i),选"w"是假(i+n),那么h-h加边就是b+n->a a+n->b ; w-h : a->b  b+n->a+n ; w-w : a->b+n  b->a+n ; h-w : b->a a+n->b+n

还有据说新郎也可能和别人通奸,新娘设为0,新郎设成n,再连一个0->n(他俩位置得固定下来啊,,,这么连边也满足我上面的推理)

注意不要用%s,貌似数据的通奸关系中间可能没有空格

还有就是和hdu1814一样的问题,if条件写一个写两个都对,我感觉应该写两个啊==

/*************
poj3648
2016.7.27
452K	0MS	C++	2038B
************/
#include 
#include
#include
#include
using namespace std;
#define maxn 8004
struct TWOSAT
{
    int n;
    vectorG[maxn*2];
    bool mark[maxn*2];
    int S[maxn*2],c;
    bool dfs(int x)
    {
        if(mark[x+n])return false;
        if(mark[x]) return true;
        mark[x]=true;
        S[c++]=x;
        for(int i=0;in=n;
        for(int i=0;i0) mark[S[--c]]=false;
                    if(!dfs(i+n)) return false;
                }
            }
        }
        return true;
    }
}solver;
int n,m;

int main()
{
   // freopen("cin.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        solver.init(n);
        for(int i=0;i



你可能感兴趣的:(—图论,———连通性,2-SAT)