Question: There was a wedding with N couples. Some of them had an affair (maybe the same sex), and one of them was a princess,
She knew the interpersonal relationships of others and asked if she could arrange a seat for N people on both sides, and the princess could not see the man with an affair at the same time.
Analysis: 2-sat. Directly follow the process.
1. Create a graph. the conflicting vertex establishes the corresponding edge (opposite to the persistent link );
2. Use the Tarjan algorithm to calculate strongly connected components and reduce points;
3. determine whether there is a solution (whether there are couples in the same set );
4. If it is true, use topological sorting to find a group of solutions and output them.
Note: The first 2sat (⊙ _ ⊙ ).
# Include <algorithm> # include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> using namespace STD; // link_list_begintypedef struct Enode {int point; enode * Next;} edge; edge * H1 [204], * H2 [204]; edge e1 [40004], E2 [40004]; int e_size1, e_size2; void link_init (void) {e_size1 = 0; e_size2 = 0; memset (H1, 0, sizeof (H1); memset (H2, 0, sizeof (H2 )); memset (E1, 0, sizeof (E1); memset (E2, 0, sizeof (E2 );} Void link_add1 (int A, int B) {e1 [e_size1]. point = B; e1 [e_size1]. next = h1 [a]; H1 [a] = & e1 [e_size1 ++];} void link_add2 (int A, int B) {e2 [e_size2]. point = B; E2 [e_size2]. next = h2 [a]; H2 [a] = & e2 [e_size2 ++];} // link_list_endint s_id, times, top; int dfn [204], low [204], use [204], STK [204], oth [204], set [204]; void Tarjan (int I, Int J) {dfn [I] = low [I] = ++ times; use [I] = 1; STK [++ top] = I; for (edge * P = H1 [I]; P = p-> next) {If (! Dfn [p-> point]) {Tarjan (p-> point, 0); low [I] = min (low [I], low [p-> point]);} else if (use [p-> point]) low [I] = min (low [I], dfn [p-> point]);} If (dfn [I] = low [I]) {s_id ++; do {J = STK [top --]; use [J] = 0; set [J] = s_id;} while (J! = I) ;}} void DFS (int I) {for (edge * P = h2 [I]; P = p-> next) if (! Use [p-> point]) {DFS (p-> point); use [I] = use [p-> point]; use [oth [I] = use [oth [p-> point];} use [I] = 1; use [oth [I] = 2 ;} void solve (int n) {// strongly connected component s_id = times = Top = 0; memset (dfn, 0, sizeof (dfn); memset (use, 0, sizeof (use); For (INT I = 0; I <2 * n; ++ I) if (! Dfn [I]) Tarjan (I, 0); // For (INT I = 0; I <n; ++ I) if (set [I * 2] = set [I * 2 + 1]) {printf ("bad luck \ n"); return ;} else {oth [set [I * 2] = set [I * 2 + 1]; oth [set [I * 2 + 1] = set [I * 2];} // topological sorting times = 0; memset (use, 0, sizeof (use )); for (INT I = 0; I <2 * n; ++ I) for (edge * P = h1 [I]; P = p-> next) if (set [p-> point]! = Set [I]) link_add2 (set [I], set [p-> point]); For (INT I = 1; I <= s_id; ++ I) if (! Use [I]) DFS (I); For (INT I = 1; I <n; ++ I) {if (I> 1) printf (""); if (use [set [I * 2] = use [set [0]) printf ("% DW", I); else printf ("% DH ", i);} printf ("\ n");} int main () {int n, m, X, Y; char character, CH2; while (~ Scanf ("% d", & N, & M) & N) {link_init (); link_add1 (0, 1); For (INT I = 0; I <m; ++ I) {scanf ("% d % C", & X, & found, & Y, & CH2 ); x <= 1; y <= 1; if (rows = 'W') x ^ = 1; if (CH2 = 'W') y ^ = 1; link_add1 (x ^ 1, Y); link_add1 (y ^ 1, x);} solve (n);} return 0 ;}