#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
o << "{";
for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif
struct Test {
void solve() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> g(n), comps(n);
vector<int> col(n), my_comp(n), num(k + 1);
iota(all(my_comp), 0);
vector<set<int>> rt(k + 1);
set<pair<int, int>> st;
vector<unordered_map<int, int>> mp(n);
for(int i = 0; i < n; i++) {
cin >> col[i];
comps[i].push_back(i);
num[col[i]]++;
rt[col[i]].insert(i);
}
for(int i = 1; i <= k; i++) {
if(num[i]) st.insert({num[i], i});
}
auto un = [&](auto&& self, int a, int b) {
a = my_comp[a];
b = my_comp[b];
if(a == b || col[a] != col[b]) return;
if(comps[a].size() + mp[a].size() < comps[b].size() + mp[b].size()) swap(a, b);
for(int v : comps[b]) {
my_comp[v] = a;
}
comps[a].insert(comps[a].end(), all(comps[b]));
if(col[a]) {
st.erase({num[col[a]], col[a]});
num[col[a]]--;
st.insert({num[col[a]], col[a]});
rt[col[a]].erase(b);
} else {
for(auto& [x, y] : mp[b]) {
if(mp[a].contains(x)) self(self, mp[a][x], y);
else mp[a][x] = y;
}
}
};
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
un(un, a, b);
}
vector<int> del(n);
while(st.size()) {
auto it = st.begin();
if(it->first > 1) {
cout << "NIE\n";
return;
}
int cc = *rt[it->second].begin();
st.erase(it);
set<pair<int, int>> st_neigh;
vector<int> zeros;
col[cc] = 0;
for(int v : comps[cc]) {
for(int to : g[v]) {
to = my_comp[to];
if(col[to]) {
st_neigh.insert({col[to], to});
} else {
zeros.push_back(to);
}
}
}
vector<pair<int, int>> neigh = vector<pair<int, int>>(all(st_neigh));
int i = 0, j = 1;
while(i < neigh.size()) {
while(j < neigh.size() && neigh[i].first == neigh[j].first) j++;
for(int k = i + 1; k < j; k++) {
un(un, neigh[i].second, neigh[k].second);
}
mp[cc][neigh[i].first] = neigh[i].second;
i = j;
}
for(int to : zeros) {
un(un, cc, to);
}
}
cout << "TAK\n";
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while(t--) {
Test tt;
tt.solve();
}
}
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 | #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) int((x).size()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) 2137 #endif struct Test { void solve() { int n, m, k; cin >> n >> m >> k; vector<vector<int>> g(n), comps(n); vector<int> col(n), my_comp(n), num(k + 1); iota(all(my_comp), 0); vector<set<int>> rt(k + 1); set<pair<int, int>> st; vector<unordered_map<int, int>> mp(n); for(int i = 0; i < n; i++) { cin >> col[i]; comps[i].push_back(i); num[col[i]]++; rt[col[i]].insert(i); } for(int i = 1; i <= k; i++) { if(num[i]) st.insert({num[i], i}); } auto un = [&](auto&& self, int a, int b) { a = my_comp[a]; b = my_comp[b]; if(a == b || col[a] != col[b]) return; if(comps[a].size() + mp[a].size() < comps[b].size() + mp[b].size()) swap(a, b); for(int v : comps[b]) { my_comp[v] = a; } comps[a].insert(comps[a].end(), all(comps[b])); if(col[a]) { st.erase({num[col[a]], col[a]}); num[col[a]]--; st.insert({num[col[a]], col[a]}); rt[col[a]].erase(b); } else { for(auto& [x, y] : mp[b]) { if(mp[a].contains(x)) self(self, mp[a][x], y); else mp[a][x] = y; } } }; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); un(un, a, b); } vector<int> del(n); while(st.size()) { auto it = st.begin(); if(it->first > 1) { cout << "NIE\n"; return; } int cc = *rt[it->second].begin(); st.erase(it); set<pair<int, int>> st_neigh; vector<int> zeros; col[cc] = 0; for(int v : comps[cc]) { for(int to : g[v]) { to = my_comp[to]; if(col[to]) { st_neigh.insert({col[to], to}); } else { zeros.push_back(to); } } } vector<pair<int, int>> neigh = vector<pair<int, int>>(all(st_neigh)); int i = 0, j = 1; while(i < neigh.size()) { while(j < neigh.size() && neigh[i].first == neigh[j].first) j++; for(int k = i + 1; k < j; k++) { un(un, neigh[i].second, neigh[k].second); } mp[cc][neigh[i].first] = neigh[i].second; i = j; } for(int to : zeros) { un(un, cc, to); } } cout << "TAK\n"; } }; int main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while(t--) { Test tt; tt.solve(); } } |
English