Katu puzzle
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:5749 |
|
Accepted:2077 |
Description
Katu puzzle is presented as a Directed GraphG(V,E) With each edgeE(A,B) Labeled by a boolean operatorOP(One of and, Or, XOR) and an integerC(0 ≤C≤ 1). One Katu is solvable if one can find each vertexVIA ValueXI(0 ≤XI≤ 1) such that for each edgeE(A,B) LabeledOPAndC, The following formula holds:
XA OP XB=C
The Calculating Rules are:
Given a Katu puzzle, your task is to determine whether it is solvable.
Input
The first line contains two integersN(1 ≤N≤ 1000) andM, (0 ≤M≤ 1,000,000) indicating the number of vertices and edges.
The followingMLines contain three IntegersA(0 ≤A<N),B(0 ≤B<N),CAnd an operatorOPEach, describing the edges.
Output
Output A line containing "yes" or "no ".
Sample Input
4 40 1 1 and1 2 1 or3 2 0 and3 0 0 XOR
Sample output
Yes
Hint
X 0 = 1,
X 1 = 1,
X 2 = 0,
X 3 = 1.
Source
Poj founder monthly contest-2008.07.27, dagger
Relatively simple 2-sat.
For the values of some expressions, see if there is any solution.
Use 2-sat to determine.
/* Poj 3678 provides the and, Or, XOR values between two pairs to determine whether a typical 2-Sat solution is available. */ # Include <Stdio. h> # Include <Iostream> # Include <Algorithm> # Include <Vector> # Include <Queue> # Include < String . H> Using Namespace STD; Const Int Maxn = 2200 ; // Bool Visit [maxn]; queue < Int > Q1, Q2; // The method for creating a vector graph is wonderful. Vector <vector < Int > Adj; // Source image // Separate the two '>' with a space in the middle. Vector <vector < Int > Radj; // Reverse Graph Vector <vector < Int > Dag; // Reverse Dag after point reduction Int N, m, CNT; Int Id [maxn], order [maxn], IND [maxn]; // Strongly connected components, access sequence, inbound Void DFS ( Int U) {visit [u] = True ; Int I, Len = Adj [u]. Size (); For (I = 0 ; I <Len; I ++ ) If (! Visit [adj [u] [I]) DFS (adj [u] [I]); Order [CNT ++] = U ;} Void RDFS ( Int U) {visit [u] = True ; Id [u] = CNT; Int I, Len = Radj [u]. Size (); For (I = 0 ; I <Len; I ++ ) If (! Visit [radj [u] [I]) RDFS (radj [u] [I]);} Void Korasaju (){ Int I; memset (visit, False , Sizeof (Visit )); For (CNT = 0 , I =0 ; I < 2 * N; I ++ ) If (! Visit [I]) DFS (I); memset (ID, 0 , Sizeof (ID); memset (visit, False , Sizeof (Visit )); For (CNT = 0 , I = 2 * N-1 ; I> = 0 ; I -- ) If (! Visit [order [I]) {CNT ++; // This must be put in front. RDFS (Order [I]) ;}} Bool Solvable (){ For ( Int I = 0 ; I <n; I ++ ) If (ID [ 2 * I] = ID [ 2 * I + 1 ]) Return False ; Return True ;} Int Main (){ Int A, B, C; Char Ch [10 ]; While (Scanf ( " % D " , & N, & M )! = EOF) {adj. Assign ( 2 * N, vector < Int > (); Radj. Assign ( 2 * N, vector < Int > ()); While (M --) {Scanf ( " % D % s " , & A, & B, & C ,& Ch ); Int I = A, j = B; If (Strcmp (CH, " And " ) = 0 ){ If (C =1 ) // Take 1 for both {Adj [ 2 * I]. push_back ( 2 * I + 1 ); Adj [ 2 * J]. push_back ( 2 * J + 1 ); Radj [ 2 * I + 1 ]. Push_back (2 * I); radj [ 2 * J + 1 ]. Push_back ( 2 * J );} Else // You cannot get 1 for both {Adj [ 2 * I + 1 ]. Push_back ( 2 *J); adj [ 2 * J + 1 ]. Push_back ( 2 * I); radj [ 2 * J]. push_back ( 2 * I + 1 ); Radj [ 2 * I]. push_back ( 2 * J + 1 );}} Else If (Strcmp (CH, " Or " ) = 0 ){ If (C = 0 ) // Both must be 0 {Adj [ 2 * I + 1 ]. Push_back ( 2 * I); adj [ 2 * J + 1 ]. Push_back ( 2 * J); radj [ 2 * I]. push_back ( 2 * I + 1 ); Radj [ 2 * J]. push_back ( 2 * J + 1 );} Else {Adj [ 2 * I]. push_back ( 2 * J + 1 ); Adj [ 2 * J]. push_back ( 2 * I + 1 ); Radj [ 2 * J + 1 ]. Push_back ( 2 *I); radj [ 2 * I + 1 ]. Push_back ( 2 * J );}} Else { If (C = 0 ) // To be the same {Adj [ 2 * I]. push_back ( 2 * J); adj [ 2 * J]. push_back ( 2 * I); adj [ 2 * I + 1 ]. Push_back ( 2 * J + 1 ); Adj [ 2 * J + 1 ]. Push_back ( 2 * I + 1 ); Radj [ 2 * I]. push_back ( 2 * J); radj [ 2 * J]. push_back ( 2 * I); radj [ 2 * I + 1 ]. Push_back ( 2 * J + 1 ); Radj [ 2 * J +1 ]. Push_back ( 2 * I + 1 );} Else {Adj [ 2 * I]. push_back ( 2 * J + 1 ); Adj [ 2 * J]. push_back ( 2 * I + 1 ); Adj [ 2 * I + 1 ]. Push_back ( 2 * J); adj [ 2 * J + 1 ]. Push_back ( 2 * I); radj [ 2 * I]. push_back ( 2 * J + 1 ); Radj [ 2 * J]. push_back (2 * I + 1 ); Radj [ 2 * I + 1 ]. Push_back ( 2 * J); radj [ 2 * J + 1 ]. Push_back ( 2 * I) ;}} korasaju (); If (Solvable () printf ( " Yes \ n " ); Else Printf ( " No \ n " );} Return 0 ;}