POJ 3648 Wedding (2-sat topology sort output any one solution)

Source: Internet
Author: User
Tags in degrees

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


Description

Up to thirty couples would attend a wedding feast, at which they would be seated on either side of a long table. The bride and groom sit at one end, opposite, and the bride wears a elaborate headdress that keeps she from SE Eing people on the same side as hers. It is considered bad luck to has a husband and wife seated on the same side of the table. Additionally, there is several pairs of people conducting adulterous relationships (both Different-sex and Same-sex Relat Ionships is possible), and it was bad luck for the bride to see both members of such a pair. Your job is to arrange people on 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, The form "4h 2w" (husband from couple 4, wife from couple 2), or "10w 4w", or "3h 1h". Couples is 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 is seated on the same side as the bride. If There is several solutions, any one would do. If There is no solution, the output a line containing "bad luck".

Sample Input

63h 7h5w 3w7h 6w8w 3w7h 3w2w 5h0 0

Sample Output

1h 2h 3w 4h 5h 6h 7h 8h 9h

Source

Waterloo Local Contest, 2007.9.29


Test instructions

A couple married and invited N couples to attend the wedding.
There was a very long table, and everyone could only sit on either side of the table,

Also meet the following requirements:

1. Each couple cannot sit on the same side

2.N may have an adulterous relationship with a couple (including male, female, female),
The relationship cannot sit opposite the bride at the same time, can sit separately, can
At the same time sitting on the side of the bride.

If there is a workable scheme, output the same side of the bride as the person.

Ps:


Solve the time to choose and the groom on the same side of the person, the output of the time to change is the bride on the same side of the person.
If I and J have an adultery, add an I to J ', J to I ' Side,
At the same time add a bride to the groom's side, indicating that the groom must be chosen.

It is easy to choose the bride on the wrong side. Because the bride may also have an affair and need to be ruled out


The code is as follows:

/*1. Based on the relationship between the theft and rape (1h and 2h have a relationship, build the side 1h->2w 2h->1w) 2. To find a strong connected component 3. Determine whether there is a solution (any couple is not in the same strong connected component, there is a solution; otherwise no solution) 4. Thumbnail map (build reverse figure) 5. Topology sorting 6. From the bottom up (as the above is a reverse graph, so the top (no penetration) downward) so far, elected to do the bride opposite, demand output, sitting on the bride's side of the * * #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> #include <iostream>using namespace std    ;//2-sat strongly connected pinch point const int MAXN = 1010;const int MAXM = 100010;struct edge{int to; int next;}    Edge[maxm];int head[maxn],tot;void init () {tot = 0; memset (head,-1,sizeof (Head));}    void Addedge (int u,int v) {edge[tot].to = v;    Edge[tot].next = Head[u]; Head[u] = tot++;} The value of the int low[maxn],dfn[maxn],stack[maxn],belong[maxn];//belong array 1~sccint index,top;int scc;bool Instack[MAXN];int num[    maxn];void Tarjan (int u)//tarjan to find strong connected component {int V;    Low[u] = dfn[u] = ++index;    stack[top++] = u;    Instack[u] = true;        for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].to; if (!            Dfn[v]) {Tarjan (v); if (LoW[u] > Low[v]) low[u] = Low[v];    } else if (Instack[v] && low[u] > Dfn[v]) low[u] = Dfn[v];        } if (low[u] = = Dfn[u]) {scc++;            do {v = stack[--top];            INSTACK[V] = false;            BELONG[V] = SCC;        num[scc]++;    } while (V! = u);    }}//determine if there is a solution bool solvable (int n)//n is the total number, need to select half {memset (dfn,0,sizeof));    memset (instack,false,sizeof (instack));    memset (num,0,sizeof (num));    Index = SCC = top = 0; for (int i = 0; i < n; i++) if (!    Dfn[i]) Tarjan (i);    for (int i = 0; i < n; i + = 2) {if (belong[i] = = Belong[i^1]) return false; } return true; Topological ordering the inverse dag plot of any set of queue<int>q1,q2;vector<vector<int> > dag;//points after the partial solution to the "R", char color[maxn];//staining    is the selected int indeg[maxn];//in degrees int cf[maxn];void topsort (int n) {dag.assign (scc+1,vector<int> ());    memset (indeg,0,sizeof (indeg));    memset (color,0,sizeof (color)); for (int u = 0; U < n;            u++)//Build Reverse indent graph for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;                if (belong[u]! = Belong[v]) {dag[belong[v]].push_back (belong[u]);            indeg[belong[u]]++;        }} for (int i = 0; i < n; i + = 2) {Cf[belong[i]] = belong[i^1];    CF[BELONG[I^1]] = belong[i];    } while (!q1.empty ()) Q1.pop ();    while (!q2.empty ()) Q2.pop ();    for (int i = 1; I <= SCC; i++) if (indeg[i] = = 0) Q1.push (i);        while (!q1.empty ()) {int u = q1.front ();        Q1.pop ();            if (color[u] = = 0) {Color[u] = ' R ';        Color[cf[u]] = ' B ';        } int sz = Dag[u].size ();            for (int i = 0; i < sz; i++) {indeg[dag[u][i]]--;        if (indeg[dag[u][i]] = = 0) q1.push (dag[u][i]);    }}}int Change (char s[]) {int ret = 0;    int i = 0; while (s[i]>= ' 0 ' &&amP        s[i]<= ' 9 ') {ret *= 10;        RET + = s[i]-' 0 ';    i++;    } if (s[i] = = ' W ')//female return 2*ret; else//male return 2*ret+1;}    int main () {int n, m;    Char s1[17], s2[17];        while (~SCANF ("%d%d", &n,&m)) {if (n = = 0 && m = = 0) break;        Init ();            while (m--) {scanf ("%s%s", S1,S2);            int u = Change (S1);            int v = change (S2);        Addedge (u^1,v);//Pay attention to the direction, simulate Addedge (v^1,u);        } addedge (1,0);            if (solvable (2*n))//Determine if there is a solution {Topsort (2*n); for (int i = 1; i < n; i++) {//Note this must be judged color[belong[if (color[belong[2*i]] = =                ' R ') printf ("%dw", I);                else printf ("%dh", I);                if (i < n-1) printf ("");            else printf ("\ n");      }} else      printf ("Bad luck\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3648 Wedding (2-sat topology sort output any one solution)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.