poj3678 Katu Puzzle 2011-12-26

Katu PuzzleTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 5143Accepted: 1815

Description

Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) such that for each edge e(a, b) labeled by op and c, the following formula holds:

 XaopXb = c

The calculating rules are:

AND01000101OR01001111XOR01001110

Given a Katu Puzzle, your task is to determine whether it is solvable.

Input

The first line contains two integers N (1 ≤ N ≤ 1000) and M,(0 ≤ M ≤ 1,000,000) indicating the number of vertices and edges.
The following M lines contain three integers a (0 ≤ a < N), b(0 ≤ b < N), c and an operator op each, 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

X0 = 1, X1 = 1, X2 = 0, X3 = 1.

 

 

 

__________________________________

  1 Program Stone;
  2 var n,m,deep,le,o,I:longint;
  3     a,b,v:longint;
  4     head:array[0..2000]of longint;
  5     next,date:array[1..4000000]of longint;
  6     stack,dfn,low,tr:array[0..2000]of longint;
  7     fs,fv:array[0..2000]of boolean;
  8  Procedure add(x,y:longint);
  9   Begin
 10     inc(le);
 11     date[le]:=y;
 12     next[le]:=head[x];
 13     head[x]:=le;
 14   end;
 15  procedure init;
 16  var i,j,k:longint;
 17      c,SP:char;
 18   begin
 19    readln(n,m);
 20    for i:=1 to m do
 21     begin
 22       readln(a,b,v,sp,c);
 23       if v=0 then
 24        case c of
 25         'A':begin
 26              add(a+n,b);
 27              add(b+n,a);
 28             end;
 29         'O':begin
 30              add(a+n,a);
 31              add(b+n,b);
 32              add(a,b);
 33              add(b,a);
 34             end;
 35         'X':begin
 36              add(a,b);
 37              add(b,a);
 38              add(a+n,b+n);
 39              add(b+n,a+n);
 40             end;
 41        end
 42       else
 43        case c of
 44         'A':begin
 45              add(a,a+n);
 46              add(b,b+n);
 47              add(a+n,b+n);
 48              add(b+n,a+n);
 49             end;
 50         'O':begin
 51              add(a,b+n);
 52              add(b,a+n);
 53             end;
 54         'X':begin
 55              add(a,b+n);
 56              add(b+n,a);
 57              add(a+n,b);
 58              add(b,a+n);
 59             end;
 60        end;
 61     end;
 62   end;
 63 
 64  procedure work(x:longint);
 65  var i:longint;
 66   begin
 67    inc(o);
 68    repeat
 69      tr[stack[le]]:=o;
 70      if stack[le]>=n then i:=stack[le]-n else i:=stack[le]+n;
 71      if tr[i]=o then begin
 72                        writeln('NO');
 73                        halt;
 74                       end;
 75      fs[stack[le]]:=false;
 76      dec(le);
 77    until stack[le+1]=x;
 78   end;
 79 
 80  function min(a,b:longint):longint;
 81   begin
 82    if a<b then min:=a else min:=b;
 83   end;
 84  Procedure tarjan(x:longint);
 85  var i:longint;
 86   begin
 87     fv[x]:=false;
 88     inc(deep);
 89     dfn[x]:=deep;
 90     low[x]:=deep;
 91     inc(le);
 92     stack[le]:=x;
 93     fs[x]:=true;
 94     i:=head[x];
 95     while i<>0 do
 96      begin
 97        if fv[date[i]] then
 98           begin
 99             tarjan(date[i]);
100             low[x]:=min(low[x],low[date[i]]);
101           end
102        else if fs[date[i]] then low[x]:=min(low[x],dfn[date[i]]);
103        i:=next[i];
104      end;
105     if dfn[x]=low[x] then work(x);
106   end;
107 Begin
108  assign(input,'input.in');reset(input);
109   init;
110   le:=0;deep:=0;o:=0;
111   fillchar(fv,sizeof(fv),true);
112   fillchar(fs,sizeof(fs),false);
113   for i:=0 to n*2-1 do
114    if fv[i] then tarjan(i);
115   writeln('YES');
116  close(input);
117 end.

 

posted on 2016-03-02 20:17  Yesphet  阅读(449)  评论(0编辑  收藏  举报