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
| #include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <queue>
using namespace std;
const int N = 3010, M = N * 5;
int m, n; int w[N], f[N]; int h[N], e[M], ne[M], idx; queue<int> q, d[N]; vector<int> in[M], out[M];
int get(char* str) { string names[] ={ "AND", "OR", "NOT", "XOR", "NAND", "NOR" }; for (int i = 0; i < 6; i ++) { if (names[i] == str) return i; } return -1; }
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx ++; d[b] ++; }
bool topsort() { q.clear(); for (int i = 1; i <= m + n; i ++) { if (!d[i]) { q[++ tt] = i; } } int cnt = 0; while(q.size()) { int t = q.front(); cnt ++; q.pop(); for (int i = h[t]; ~i; i = ne[i]) { int j = e[i]; if ( -- d[j] == 0) { q.push(j); } } } return cnt == m + n - 1; }
int main() { int T; scanf("%d", &T); while(T --) { scanf("%d%d", &m, &n); memset(h, -1, sizeof h); idx = 0; memset(d, 0, sizeof d); char str[100]; for (int i = 1; i <= n; i ++) { int cnt; scanf("%s%d", str, &cnt); f[m + i] = get(str); while(cnt --) { scanf("%s", str); int t = atoi(str + 1); if (str[0] == 'I') add(t, m + 1); else add(m + t, m + 1); } } int Q; scanf("%d", &Q); for (int i = 0; i < Q;i ++) { in[i].clear(); for (int j 0; j < m; j ++) { int x; scanf("%d", &x); in[i].push_back(x); } } for (int i = 0; i < Q;i ++) { out[i].clear(); int cnt; scanf("%d", &cnt); while(cnt --) { int x; scanf("%d", &x); out[i].push_back(x); } } if (!topsort()) { cout << "LOOP" << endl; } else { for (int i = 0; i < Q; i ++) { for (int j = 0; j < m; j ++) { w[j + 1] = in[i][j]; } for (int j = m + 1; j < m + n; j ++) { if (f[j] == 0 || f[j] == 5) w[j] = 1; else w[j] = 0; } } } } }
|