1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<cmath> 8 #include<stack> 9 using namespace std; 10 #define Maxn 1010 11 #define Maxm 1000010 12 13 char s[10]; 14 int n,m; 15 16 struct node 17 { 18 int x,y,next; 19 }t[4*Maxm];int len; 20 int first[2*Maxn]; 21 22 void ins(int x,int y) 23 { 24 t[++len].x=x;t[len].y=y; 25 t[len].next=first[x];first[x]=len; 26 } 27 28 bool mark[2*Maxn]; 29 int S[2*Maxn]; 30 31 bool dfs(int x) 32 { 33 if(mark[x^1]) return 0; 34 if(mark[x]) return 1; 35 S[++S[0]]=x;mark[x]=1; 36 for(int i=first[x];i;i=t[i].next) 37 { 38 int y=t[i].y; 39 if(!dfs(y)) return 0; 40 } 41 return 1; 42 } 43 44 bool tsat() 45 { 46 for(int i=0;i<n;i++) 47 { 48 if(!mark[2*i]&&!mark[2*i+1]) 49 { 50 S[0]=0; 51 if(!dfs(2*i)) 52 { 53 while(S[0]) mark[S[S[0]--]]=0; 54 if(!dfs(2*i+1)) return 0; 55 } 56 } 57 } 58 return 1; 59 } 60 61 int main() 62 { 63 scanf("%d%d",&n,&m); 64 len=0; 65 memset(first,0,sizeof(first)); 66 for(int i=1;i<=m;i++) 67 { 68 int x,y,z; 69 scanf("%d%d%d",&x,&y,&z); 70 scanf("%s",s); 71 if(s[0]=='A') 72 { 73 if(z==1) 74 { 75 ins(2*x,2*y);ins(2*x,2*y+1); 76 ins(2*y,2*x);ins(2*y,2*x+1); 77 } 78 else 79 { 80 ins(2*x+1,2*y); 81 ins(2*y+1,2*x); 82 } 83 } 84 else if(s[0]=='O') 85 { 86 if(z==1) 87 { 88 ins(2*x,2*y+1); 89 ins(2*y,2*x+1); 90 } 91 else 92 { 93 ins(2*x+1,2*y);ins(2*x+1,2*y+1); 94 ins(2*y+1,2*x);ins(2*y+1,2*x+1); 95 } 96 } 97 else if(s[0]=='X') 98 { 99 if(z==1) 100 { 101 ins(2*x,2*y+1);ins(2*x+1,2*y); 102 ins(2*y,2*x+1);ins(2*y+1,2*x); 103 } 104 else 105 { 106 ins(2*x,2*y);ins(2*x+1,2*y+1); 107 ins(2*y,2*x);ins(2*y+1,2*x+1); 108 } 109 } 110 } 111 if(tsat()) printf("YES "); 112 else printf("NO "); 113 return 0; 114 }
2-SAT
2016-11-17 22:00:10