Poj 3678 Katu puzzle

Source: Internet
Author: User
Katu puzzletime limit: 1000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 3678
64-bit integer Io format: % LLD Java class name: Main

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:

And 0 1
0 0 0
1 0 1
Or 0 1
0 0 1
1 1 1
XOR 0 1
0 0 1
1 1 0

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
Sourcepoj founder monthly contest-2008.07.27 problem solving: 2sat
  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <cmath>  5 #include <algorithm>  6 #include <climits>  7 #include <vector>  8 #include <queue>  9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 2010; 18 struct arc{ 19     int to,next; 20     arc(int x = 0,int y = -1){ 21         to = x; 22         next = y; 23     } 24 }; 25 arc e[4000010]; 26 int head[maxn],dfn[maxn],low[maxn],belong[maxn]; 27 int tot,cnt,scc,n,m; 28 bool instack[maxn]; 29 stack<int>stk; 30 void add(int u,int v){ 31     e[tot] = arc(v,head[u]); 32     head[u] = tot++; 33 } 34 void init(){ 35     for(int i = 0; i < maxn; i++){ 36         belong[i] = 0; 37         low[i] = dfn[i] = 0; 38         head[i] = -1; 39         instack[i] = false; 40     } 41     while(!stk.empty()) stk.pop(); 42     tot = cnt = scc = 0; 43 } 44 void tarjan(int u){ 45     dfn[u] = low[u] = ++cnt; 46     instack[u] = true; 47     stk.push(u); 48     for(int i = head[u]; ~i; i = e[i].next){ 49         if(!dfn[e[i].to]){ 50             tarjan(e[i].to); 51             if(low[e[i].to] < low[u]) low[u] = low[e[i].to]; 52         }else if(instack[e[i].to] && dfn[e[i].to] < low[u]) 53             low[u] = dfn[e[i].to]; 54     } 55     if(dfn[u] == low[u]){ 56         scc++; 57         int v; 58         do{ 59             v = stk.top(); 60             stk.pop(); 61             instack[v] = false; 62             belong[v] = scc; 63         }while(v != u); 64     } 65 } 66 bool solve(){ 67     for(int i = 0; i < (n<<1); i++) 68         if(!dfn[i]) tarjan(i); 69     for(int i = 0; i < n; i++) 70         if(belong[i] == belong[i+n]) return false; 71     return true; 72 } 73 int main() { 74     char op[5]; 75     int u,v,c; 76     while(~scanf("%d %d",&n,&m)){ 77         init(); 78         while(m--){ 79             scanf("%d %d %d %s",&u,&v,&c,op); 80             if(op[0] == ‘A‘){ 81                 if(c){ 82                     add(u+n,v+n); 83                     add(v+n,u+n); 84                     add(u,u+n); 85                     add(v,v+n); 86                 }else{ 87                     add(u+n,v); 88                     add(v+n,u); 89                 } 90             }else if(op[0] == ‘O‘){ 91                 if(c){ 92                     add(u,v+n); 93                     add(v,u+n); 94                 }else{ 95                     add(u,v); 96                     add(v,u); 97                     add(u+n,u); 98                     add(v+n,v); 99                 }100             }else if(op[0] == ‘X‘){101                 if(c){102                     add(u,v+n);103                     add(v,u+n);104                     add(u+n,v);105                     add(v+n,u);106                 }else{107                     add(u,v);108                     add(v,u);109                     add(u+n,v+n);110                     add(v+n,u+n);111                 }112             }113         }114         printf("%s\n",solve()?"YES":"NO");115     }116     return 0;117 }
View code

 

Poj 3678 Katu puzzle

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.