#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<typename T>
auto &operator<<(auto &o, pair<T, T> p) {
return o << "(" << p.first << ", " << p.second <<")";
}
auto operator<<(auto &o, auto x)-> decltype(x.end(), o) {
o << "{";int i = 0;
for(auto e : x) o << ", "+!i++<<e;
return o <<"}";
}
#define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif
struct dsu {
vector<int> A;
vector<int> r;
vector<int> comp_cnt;
vector<set<pair<int, int>>> teams;
vector<int> ready;
dsu (int n, int k, const vector<int> &_A) : A(_A), r(n, -1), comp_cnt(k), teams(n) {
for (int i = 0; i < n; ++i) {
teams[i].emplace(A[i], i);
++comp_cnt[A[i]];
}
for (int i = 0; i < k; ++i) {
if (comp_cnt[i] == 1) {
ready.push_back(i);
}
}
}
int find(int u) {
return r[u] < 0 ? u : r[u] = find(r[u]);
}
void connect(int u, int v) {
debug("connect", u, v);
int a = find(u);
debug(A);
auto it = teams[a].lower_bound(pair{A[v], 0});
if (it != teams[a].end() && it->first == A[v]) {
if (merge(v, it->second)) {
if (--comp_cnt[A[v]] == 1) {
ready.push_back(A[v]);
}
}
}
else {
teams[a].emplace(A[v], v);
}
debug(comp_cnt, teams, ready);
}
bool merge(int u, int v) {
debug("merge", u, v);
int a = find(u), b = find(v);
if (a == b) { return false; }
if (r[a] < r[b]) { swap(a, b); }
r[b] += r[a];
r[a] = b;
debug(comp_cnt, teams, ready);
return true;
}
void merge2(int u, int v) {
debug("merge2", u, v);
int a = find(u), b = find(v);
if (a == b) { return; }
if (r[a] < r[b]) { swap(a, b); }
r[b] += r[a];
r[a] = b;
for (auto [t, i] : teams[a]) {
if (comp_cnt[t] <= 1) { continue; }
auto it = teams[b].lower_bound(pair{t, 0});
if (it != teams[b].end() && it->first == t) {
if (merge(i, it->second)) {
if (--comp_cnt[t] == 1) {
ready.push_back(t);
}
}
}
else {
teams[b].emplace(t, i);
}
}
debug(comp_cnt, teams, ready);
}
};
void test_case() {
int n, m, k;
cin >> n >> m >> k;
vector<int> A(n);
vector<vector<int>> where(k);
int visible = 0;
for (auto [i, a] : views::enumerate(A)) {
cin >> a;
--a;
visible += where[a].empty();
where[a].push_back((int)i);
}
dsu d(n, k, A);
vector<vector<int>> adj(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
adj[u].push_back(v);
adj[v].push_back(u);
if (A[u] == A[v]) {
d.connect(u, v);
d.merge2(u, v);
}
}
vector<bool> vis(k);
for (int i = 0; i < visible; ++i) {
if (d.ready.empty()) {
cout << "NIE\n";
return;
}
auto t = d.ready.back();
d.ready.pop_back();
assert(!vis[t]);
vis[t] = true;
for (auto u : where[t]) {
A[u] = -1;
}
for (auto u : where[t]) {
for (auto v : adj[u]) {
if (A[v] == -1) {
d.merge2(u, v);
}
else {
d.connect(u, v);
}
}
}
}
cout << "TAK\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
test_case();
}
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG template<typename T> auto &operator<<(auto &o, pair<T, T> p) { return o << "(" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif struct dsu { vector<int> A; vector<int> r; vector<int> comp_cnt; vector<set<pair<int, int>>> teams; vector<int> ready; dsu (int n, int k, const vector<int> &_A) : A(_A), r(n, -1), comp_cnt(k), teams(n) { for (int i = 0; i < n; ++i) { teams[i].emplace(A[i], i); ++comp_cnt[A[i]]; } for (int i = 0; i < k; ++i) { if (comp_cnt[i] == 1) { ready.push_back(i); } } } int find(int u) { return r[u] < 0 ? u : r[u] = find(r[u]); } void connect(int u, int v) { debug("connect", u, v); int a = find(u); debug(A); auto it = teams[a].lower_bound(pair{A[v], 0}); if (it != teams[a].end() && it->first == A[v]) { if (merge(v, it->second)) { if (--comp_cnt[A[v]] == 1) { ready.push_back(A[v]); } } } else { teams[a].emplace(A[v], v); } debug(comp_cnt, teams, ready); } bool merge(int u, int v) { debug("merge", u, v); int a = find(u), b = find(v); if (a == b) { return false; } if (r[a] < r[b]) { swap(a, b); } r[b] += r[a]; r[a] = b; debug(comp_cnt, teams, ready); return true; } void merge2(int u, int v) { debug("merge2", u, v); int a = find(u), b = find(v); if (a == b) { return; } if (r[a] < r[b]) { swap(a, b); } r[b] += r[a]; r[a] = b; for (auto [t, i] : teams[a]) { if (comp_cnt[t] <= 1) { continue; } auto it = teams[b].lower_bound(pair{t, 0}); if (it != teams[b].end() && it->first == t) { if (merge(i, it->second)) { if (--comp_cnt[t] == 1) { ready.push_back(t); } } } else { teams[b].emplace(t, i); } } debug(comp_cnt, teams, ready); } }; void test_case() { int n, m, k; cin >> n >> m >> k; vector<int> A(n); vector<vector<int>> where(k); int visible = 0; for (auto [i, a] : views::enumerate(A)) { cin >> a; --a; visible += where[a].empty(); where[a].push_back((int)i); } dsu d(n, k, A); vector<vector<int>> adj(n); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); if (A[u] == A[v]) { d.connect(u, v); d.merge2(u, v); } } vector<bool> vis(k); for (int i = 0; i < visible; ++i) { if (d.ready.empty()) { cout << "NIE\n"; return; } auto t = d.ready.back(); d.ready.pop_back(); assert(!vis[t]); vis[t] = true; for (auto u : where[t]) { A[u] = -1; } for (auto u : where[t]) { for (auto v : adj[u]) { if (A[v] == -1) { d.merge2(u, v); } else { d.connect(u, v); } } } } cout << "TAK\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { test_case(); } return 0; } |
English