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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include <bits/stdc++.h> #define N 100010 #define M 200010 using namespace std;
struct Edge { int to, ne; } edges[M], dag[M];
int h[N], hs[N], idx = 0, idx1 = 0; int scc_cnt = 0, dfs_cnt = 0; int dfn[N], low[N]; int scc_id[N]; vector<int> scc[N]; stack<int> stk; bool st[N]; bool in_stk[N]; bool color[N], scc_color[N]; int deg[N];
void add(int u, int v) { idx++; edges[idx].to = v; edges[idx].ne = h[u]; h[u] = idx; }
void shrink(int u, int v) { idx1++; dag[idx1].to = v; dag[idx1].ne = hs[u]; hs[u] = idx1; }
bool tarjan(int u) { stk.push(u); in_stk[u] = true; dfn[u] = low[u] = ++dfs_cnt;
for (int i = h[u]; ~i; i = edges[i].ne) { int j = edges[i].to; if (!dfn[j]) { tarjan(j); low[u] = min(low[u], low[j]); } else if (in_stk[j]) { low[u] = min(low[u], dfn[j]); } } if (dfn[u] == low[u]) { scc_cnt++; int t; do { t = stk.top(); stk.pop(); in_stk[t] = false; scc_id[t] = scc_cnt; scc[scc_cnt].push_back(t); } while (t != u); for (int i = 1; i < scc[scc_cnt].size(); i++) { if (color[scc[scc_cnt][i]] ^ color[scc[scc_cnt][i - 1]]) return false; } scc_color[scc_cnt] = color[scc[scc_cnt][0]]; } return true; }
void init() { idx = 0; idx1 = 0; while (!stk.empty()) stk.pop(); for (int i = 1; i <= scc_cnt; i++) scc[i].clear(); scc_cnt = dfs_cnt = 0; memset(st, false, sizeof st); memset(scc_id, 0, sizeof scc_id); memset(dfn, 0, sizeof dfn); memset(low, 0, sizeof low); memset(in_stk, false, sizeof in_stk); memset(deg, 0, sizeof deg); memset(h, -1, sizeof h); memset(hs, -1, sizeof hs); }
int getFirstBlack() { queue<int> topo; for (int i = 1; i <= scc_cnt; i++) { if (!deg[i]) topo.push(i); } while (!topo.empty()) { int t = topo.front(); topo.pop(); if (scc_color[t]) return t; for (int i = hs[t]; ~i; i = dag[i].ne) { int j = dag[i].to; if (--deg[j] == 0) topo.push(j); } } return 0; }
bool dfs(int u) { bool ret = scc_color[u]; st[u] = true; for (int i = hs[u]; ~i; i = dag[i].ne) { int j = dag[i].to; if (st[j]) continue; ret &= dfs(j); } return ret; }
bool check() { for (int i = 1; i <= scc_cnt; i++) { if (scc_color[i] && !st[i]) return false; } return true; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int t, m, n; cin >> t; while (t--) { init(); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> color[i]; int u, v; for (int i = 1; i <= m; i++) { cin >> u >> v; add(u, v); } bool tmp = true; for (int i = 1; i <= n; i++) { if (!dfn[i]) { if (!tarjan(i)) { tmp = false; cout << 'N'; break; } } } if (!tmp) continue; bool b2w = true; for (int i = 1; i <= scc_cnt; i++) { for (int a: scc[i]) { for (int j = h[a]; ~j; j = edges[j].ne) { int k = edges[j].to; if (scc_id[a] != scc_id[k]) { shrink(scc_id[a], scc_id[k]); b2w &= !scc_color[scc_id[k]] && scc_color[scc_id[a]]; deg[scc_id[k]]++; } } } } int start = getFirstBlack(); if (dfs(start) && check()) cout << 'A'; else if (!start && !color[1]) cout << 'B'; else if (scc_cnt == 2 && scc_color[1] & scc_color[2]) cout << 'B'; else if (scc_cnt == 2 && b2w) cout << 'B'; else cout << 'N'; } return 0; }
|