/* -*- coding: utf-8 -*- * * 2308.cc: No.2308 [Cherry 5th Tune B] もしかして、真? - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; enum { AND, OR, XOR, IMP }; /* typedef */ struct UFT { vector links, ranks, sizes; UFT() {} void init(int n) { links.resize(n); for (int i = 0; i < n; i++) links[i] = i; ranks.assign(n, 1); sizes.assign(n, 1); } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; template struct BIT { int n; vector bits; BIT() {} BIT(int _n) { init(_n); } void init(int _n) { n = _n; bits.assign(n + 1, 0); } T sum(int x) { x = min(x, n); T s = 0; while (x > 0) { s += bits[x]; x -= (x & -x); } return s; } void add(int x, T v) { if (x <= 0) return; while (x <= n) { bits[x] += v; x += (x & -x); } } }; /* global variables */ bool fs[MAX_N]; int ops[MAX_N], ss[MAX_N]; /* subroutines */ bool func(int op, bool a, bool b) { switch (op) { case AND: return a && b; case OR: return a || b; case XOR: return a ^ b; case IMP: return ! a || b; } return false; } /* main */ int main() { int tn; scanf("%d", &tn); while (tn--) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { char s[8]; scanf("%s", s); fs[i] = (s[0] == 'T'); } for (int i = 0; i < n - 1; i++) { char s[8]; scanf("%s", s); ops[i] = (s[0] == 'a') ? AND : (s[0] == 'o') ? OR : (s[0] == 'x') ? XOR : IMP; } for (int i = 0; i < n - 1; i++) scanf("%d", ss + i), ss[i]--; UFT uft; uft.init(n); BIT bit; bit.init(n - 1); for (int i = 0; i < n - 1; i++) bit.add(i + 1, 1); for (int i = 0; i < n - 1; i++) { int x0 = -1, x1 = n - 1; while (x0 + 1 < x1) { int x = (x0 + x1) / 2; if (bit.sum(x + 1) >= ss[i]) x1 = x; else x0 = x; } int u = uft.root(x1), v = uft.root(x1 + 1); int r = uft.merge(u, v); fs[r] = func(ops[x1], fs[u], fs[v]); bit.add(x1 + 1, -1); } if (fs[uft.root(0)]) puts("True"); else puts("False"); } return 0; }