#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif
/// We can keep only one find & union by creating white vertices on each edge
/// then on fill we don't need to switch anything to white, just merge all neighbors.
/// It means that our graph is also bipartite.
/// So we never join white and colorful nodes.
/// And colorful nodes also have empty reps so it's free.
void solve() {
int n,m,k;
cin >> n >> m >> k;
int N = n+m;
vector<int> C(N+1, k+1);
vector<vi> V(N+1);
vector<int> counts(k+2);
vector<vi> where(k+2);
for(int i = 1; i <= n; i++) {
cin >> C[i];
counts[C[i]]++;
where[C[i]].pb(i);
}
for(int i = n + 1; i <= N; i++) {
int a,b;
cin >> a >> b;
V[a].pb(i);
V[i].pb(a);
V[b].pb(i);
V[i].pb(b);
}
// Variables for f&u
vi R(N+1);
/// representantive neighbor of each color;
vector<unordered_map<int, int>> reps(N+1);
vi work;
auto Find = [&R](const auto& self, int x) -> int {
if (R[x] != x) R[x] = self(self, R[x]);
return R[x];
};
auto Union = [&Find, &R, &reps, &work, &C, &counts, &where](const auto& self, int a, int b) -> void {
a = Find(Find, a);
b = Find(Find, b);
if (a == b) return;
counts[C[a]]--;
if(counts[C[a]] == 1) { // Won't happen for whites as they start with counts == 0
work.insert(work.end(), all(where[C[a]]));
}
if (reps[a].size() < reps[b].size()) swap(a,b);
R[b] = a;
// Won't happen for colorful as they do not have reps
for(auto [c, v]: reps[b]) {
if (reps[a].find(c) != reps[a].end()) {
self(self, reps[a][c], v);
} else {
reps[a][c] = v;
}
}
reps[b].clear();
};
for(int i = 1; i <= N; i++) R[i] = i;
for(int i = 1; i <= k; i++) {
if (counts[i] == 1) {
work.pb(where[i][0]);
}
}
for (int i = n + 1; i <= N; i++) {
int a = V[i][0];
int b = V[i][1];
if (C[a] == C[b]) {
reps[i][C[a]] = a;
Union(Union, a, b);
} else {
reps[i][C[a]] = a;
reps[i][C[b]] = b;
}
}
int res = n;
while(!work.empty()) {
res--;
int v = work.back();
work.pop_back();
for(int i = 1; i < V[v].size(); i++) {
Union(Union, V[v][0], V[v][i]);
}
}
cout << ((res == 0) ? "TAK" : "NIE") << "\n";
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
int t;
cin >> t;
while(t--) 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 133 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; } #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define deb(...) 0 #endif /// We can keep only one find & union by creating white vertices on each edge /// then on fill we don't need to switch anything to white, just merge all neighbors. /// It means that our graph is also bipartite. /// So we never join white and colorful nodes. /// And colorful nodes also have empty reps so it's free. void solve() { int n,m,k; cin >> n >> m >> k; int N = n+m; vector<int> C(N+1, k+1); vector<vi> V(N+1); vector<int> counts(k+2); vector<vi> where(k+2); for(int i = 1; i <= n; i++) { cin >> C[i]; counts[C[i]]++; where[C[i]].pb(i); } for(int i = n + 1; i <= N; i++) { int a,b; cin >> a >> b; V[a].pb(i); V[i].pb(a); V[b].pb(i); V[i].pb(b); } // Variables for f&u vi R(N+1); /// representantive neighbor of each color; vector<unordered_map<int, int>> reps(N+1); vi work; auto Find = [&R](const auto& self, int x) -> int { if (R[x] != x) R[x] = self(self, R[x]); return R[x]; }; auto Union = [&Find, &R, &reps, &work, &C, &counts, &where](const auto& self, int a, int b) -> void { a = Find(Find, a); b = Find(Find, b); if (a == b) return; counts[C[a]]--; if(counts[C[a]] == 1) { // Won't happen for whites as they start with counts == 0 work.insert(work.end(), all(where[C[a]])); } if (reps[a].size() < reps[b].size()) swap(a,b); R[b] = a; // Won't happen for colorful as they do not have reps for(auto [c, v]: reps[b]) { if (reps[a].find(c) != reps[a].end()) { self(self, reps[a][c], v); } else { reps[a][c] = v; } } reps[b].clear(); }; for(int i = 1; i <= N; i++) R[i] = i; for(int i = 1; i <= k; i++) { if (counts[i] == 1) { work.pb(where[i][0]); } } for (int i = n + 1; i <= N; i++) { int a = V[i][0]; int b = V[i][1]; if (C[a] == C[b]) { reps[i][C[a]] = a; Union(Union, a, b); } else { reps[i][C[a]] = a; reps[i][C[b]] = b; } } int res = n; while(!work.empty()) { res--; int v = work.back(); work.pop_back(); for(int i = 1; i < V[v].size(); i++) { Union(Union, V[v][0], V[v][i]); } } cout << ((res == 0) ? "TAK" : "NIE") << "\n"; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); int t; cin >> t; while(t--) solve(); } |
English