标签:
理清逻辑关系即可。
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int N = 1000; 8 const int M = 5000000; 9 int head[N * 2]; 10 int s[N * 2]; 11 bool mark[N * 2]; 12 int n, m, e, c; 13 14 struct Edge 15 { 16 int v, next; 17 } edge[M]; 18 19 void addEdge( int u, int v ) 20 { 21 edge[e].v = v; 22 edge[e].next = head[u]; 23 head[u] = e++; 24 } 25 26 void init() 27 { 28 e = 0; 29 memset( head, -1, sizeof(head) ); 30 memset( mark, false, sizeof(mark) ); 31 } 32 33 bool dfs( int u ) 34 { 35 if ( mark[u ^ 1] ) return false; 36 if ( mark[u] ) return true; 37 mark[u] = true; 38 s[c++] = u; 39 for ( int i = head[u]; i != -1; i = edge[i].next ) 40 { 41 int v = edge[i].v; 42 if ( !dfs(v) ) return false; 43 } 44 return true; 45 } 46 47 bool solve() 48 { 49 for ( int i = 0; i < 2 * n; i += 2 ) 50 { 51 if ( !mark[i] && !mark[i + 1] ) 52 { 53 c = 0; 54 if ( !dfs(i) ) 55 { 56 while ( c ) 57 { 58 c--; 59 mark[s[c]] = false; 60 } 61 if ( !dfs( i + 1 ) ) return false; 62 } 63 } 64 } 65 return true; 66 } 67 68 int main () 69 { 70 while ( scanf("%d%d", &n, &m) != EOF ) 71 { 72 init(); 73 int x, y, z; 74 char op[11]; 75 while ( m-- ) 76 { 77 scanf("%d%d%d%s", &x, &y, &z, op); 78 x = x << 1, y = y << 1; 79 if ( op[0] == ‘A‘ ) 80 { 81 if ( z == 0 ) 82 { 83 addEdge( x ^ 1, y ); 84 addEdge( y ^ 1, x ); 85 } 86 else 87 { 88 addEdge( x, x ^ 1 ); 89 addEdge( y, y ^ 1 ); 90 } 91 } 92 else if ( op[0] == ‘O‘ ) 93 { 94 if ( z == 0 ) 95 { 96 addEdge( x ^ 1, x ); 97 addEdge( y ^ 1, y ); 98 } 99 else 100 { 101 addEdge( x, y ^ 1 ); 102 addEdge( y, x ^ 1 ); 103 } 104 } 105 else if ( op[0] == ‘X‘ ) 106 { 107 if ( z == 0 ) 108 { 109 addEdge( x, y ); 110 addEdge( x ^ 1, y ^ 1 ); 111 addEdge( y, x ); 112 addEdge( y ^ 1, x ^ 1 ); 113 } 114 else 115 { 116 addEdge( x, y ^ 1 ); 117 addEdge( x ^ 1, y ); 118 addEdge( y, x ^ 1 ); 119 addEdge( y ^ 1, x ); 120 } 121 } 122 } 123 if ( solve() ) 124 { 125 printf("YES\n"); 126 } 127 else 128 { 129 printf("NO\n"); 130 } 131 } 132 return 0; 133 }
标签:
原文地址:http://www.cnblogs.com/huoxiayu/p/4699831.html