/* 2-Sat question. Question: If a couple gets married, n-1 couples to calculate their own n pairs, numbered 0 ~~ N-1: they numbered 0. All of them sat on both sides of the table. The bride did not want to see the opposite person having a husband-wife relationship or a rape relationship. If there was a solution, a group of solutions would be output, and the bad luck idea would be output: 1. build a Graph Based on the ing relationship (1 h and 2 h are in a ing relationship, 1 H-> 2 W 2 h-> 1 W) 2. require strongly connected component 3. judgment has no solution (any couple is not in the same strong connected component, there is a solution; otherwise there is no solution) 4. scale down a point to create a graph (reverse graph) 5. topology Sorting 6. the bottom-up solution (because the above is an undirected graph, so the top (no inbound) Down) is now selected as the opposite of the bride, and the output is required, */# include <stdio. h> # include <string. h >#include <queue >#include <stack> using namespace STD; struct edge {int Yong; int V [1000000]; int next [1000000]; int head [1000000]; edge () {clear ();} voi D clear () {Yong = 1; memset (Head, 0, sizeof (head);} void add (int u, int W) {v [Yong] = W; next [Yong] = head [u]; head [u] = Yong; Yong ++;} E1, E2; int dfn [1000], low [1000]; int n, m, index, SCC; queue <int> q; stack <int> SS; int INS [1000], belong [1000], DUI [1000], IND [1000], Fang [1000]; void Tarjan (int u) // returns the strongly connected component {int I, V; dfn [u] = low [u] = index ++; INS [u] = 1; SS. push (U); // The queue is mistakenly written as for (I = e1.head [u]; I; I = e1.next [I]) {v = e1.v [I]; if (dfn [v] = 0) {T Arjan (V); If (low [v] <low [u]) low [u] = low [v];} else if (INS [v] & dfn [v] <low [u]) low [u] = dfn [v];} if (low [u] = dfn [u]) {do {v = ss. top (); SS. pop (); INS [v] = 0; belong [v] = SCC; // label the strongly connected component} while (V! = U); SCC ++ ;}} void topsort () // Topology Sorting and solving {int V, U; while (! Q. empty () Q. pop (); int I; for (I = 0; I <SCC; I ++) {If (IND [I] = 0) // a node with 0 inputs enters the queue Q. push (I) ;}while (! Q. Empty () {v = Q. Front (); q. Pop (); If (! Fang [v]) // 0-level node coloring {// Fang [I] indicates that the strongly connected component labeled I is selected or deleted. Fang [v] = 1; // indicates selecting Fang [DUI [v] = 2; // indicates deletion} for (I = e2.head [v]; I; I = e2.next [I]) {u = e2.v [I]; If (-- ind [u] = 0) // subtract the inbound level, and enter {q. push (u) ;}}} int main () {int I, j, A, B, V; char C1, C2; while (scanf ("% d", & N, & M), m + n) {// initialize e1.clear (); e2.clear (); // create the source image for (I = 1; I <= m; I ++) {scanf ("% d % C", & A, & C1, & B, & C2); e1.add (C1 = 'H '? A * 2 + 1: A * 2, C2 = 'H '? B * 2: B * 2 + 1); e1.add (C2 = 'H '? B * 2 + 1: B * 2, C1 = 'H '? A * 2: A * 2 + 1); // handle it carefully here} e1.add (); // required for the groom // search for the Strongly Connected Component index = 1; // dfn [] Mark SCC = 0; // Number of strongly connected components, counted from 0 memset (dfn, 0, sizeof (dfn); memset (INS, 0, sizeof (INS); for (I = 0; I <2 * n; I ++) {If (! Dfn [I]) Tarjan (I) ;}// you can determine that there is no solution for (I = 0; I <n; I ++) {// belong [I] indicates the ID of the strongly connected component to which I belongs if (belong [2 * I] = belong [2 * I + 1]) // if a couple have no break in the same strongly connected component, DUI [belong [2 * I + 1] = belong [2 * I]; // records the number of strongly connected components to which the spouse belongs. When solving the problem, DUI [belong [2 * I] = belong [2 * I + 1];} if (I! = N) {printf ("bad luck \ n"); continue;} // construct a reverse point reduction graph memset (IND, 0, sizeof (IND )); for (I = 0; I <2 * n; I ++) {for (j = e1.head [I]; j = e1.next [J]) {v = e1.v [J]; If (belong [I]! = Belong [v]) {e2.add (belong [v], belong [I]); // note that the order of this field is ind [belong [I] ++; // statistics inbound }}// memset (Fang, 0, sizeof (Fang); topsort (); // output solution for (I = 1; I <n; I ++) {If (Fang [belong [2 * I] = 1) printf ("% DH", I); else printf ("% DW", I);} printf ("\ n");} return 0 ;}