1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   | #include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2010, M = N * N;
  int n, m; int head[N], ne[M], edge[M], tot = 1; stack<int> stk; bool ins[N]; int c[N]; int dfn[N], low[N], num, cnt; vector<int> scc[N];
  void add(int a, int b) {     edge[++tot] = b;     ne[tot] = head[a], head[a] = tot; }
  void build() {     for (int i = 1; i <= m; i++)     {         int a, b, c;         string op;         cin >> a >> b >> c >> op;         a++, b++;         if (op == "AND")         {             if (c == 1)             {                  add(a, a + n);                 add(b, b + n);             }             else             {                  add(a + n, b);                 add(b + n, a);             }         }         else if (op == "OR")         {             if (c == 1)             {                  add(a, b + n);                 add(b, a + n);             }             else             {                  add(a + n, a);                 add(b + n, b);             }         }         else         {             if (c == 1)             {                  add(a, b + n);                 add(a + n, b);                 add(b, a + n);                 add(b + n, a);             }             else             {                  add(a, b);                 add(a + n, b + n);                 add(b, a);                 add(b + n, a + n);             }         }     } }
  void tarjan(int u) {     dfn[u] = low[u] = ++num;     stk.push(u), ins[u] = true;     for (int i = head[u]; ~i; i = ne[i])     {         int v = edge[i];         if (!dfn[v])          {             tarjan(v);             low[u] = min(low[u], low[v]);         }         else if (ins[v])              low[u] = min(low[u], dfn[v]);     }     if (dfn[u] == low[u])      {         cnt++;         int x;         do         {             x = stk.top(), ins[x] = false;             stk.pop();             c[x] = cnt, scc[cnt].push_back(x);         } while (x != u);     } }
  void output() {     for (int i = 1; i <= n; i++)     {         if (c[i] == c[i + n])         {             cout << "NO";             return;         }     }     cout << "YES"; }
  int main() {     memset(head, -1, sizeof(head));     num = cnt = 0;     cin >> n >> m;     build();     for (int i = 1; i <= 2 * n; i++)         if (!dfn[i])             tarjan(i);     output(); }
   |