#include <bits/stdc++.h>
#define pii pair<int, int>
#define For(i, l, r) for (int i=l;(l<=r?i<=r:i>=r);(l<=r?i++:i--))
#define DEBUG
#ifdef DEBUG
auto operator<<(auto &o,auto p)->decltype(p.first,o){return o<<'('<<p.first<<", "<<p.second<<')';}
auto operator<<(auto &o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto &e:x)o<<(", ")+i<<e,i=0;return o<<'}';}
#define LOG(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X);
#else
#define LOG(x...)(void)0
#endif
//#define int long long
using namespace std;
const int M = 100005;
int n, m, k;
int rep[M], siz[M], col_count[M], type[M];
vector<int> g[M], types[M];
queue<int> q;
map<int, int> reachable[M];
void reset(){
for (int i = 1; i <= k; i++){
col_count[i] = 0;
types[i].clear();
reachable[i].clear();
}
col_count[0] = 5 * n;
for (int i = 0; i <= n; i++){
g[i].clear();
siz[i] = 1;
rep[i] = i;
}
q = {};
}
int Find(int a){
if (a == rep[a])
return a;
return rep[a] = Find(rep[a]);
}
int Union(int a, int b);
void merge_maps(map<int, int> &a, map<int, int> &b){
for (auto [t, id]: b){
if (type[id] != t)
continue;
if (a.count(t) && type[a[t]] == t){
Union(a[t], id);
}
else {
a[t] = id;
}
}
b.clear();
}
int Union(int a, int b){
assert(type[a] == type[b]);
a = Find(a);
b = Find(b);
if (a == b)
return 0;
if (siz[a] < siz[b])
swap(a, b);
rep[b] = a;
siz[a] += siz[b];
if (type[a] == 0){
if (reachable[a].size() >= reachable[b].size())
merge_maps(reachable[a], reachable[b]);
else {
merge_maps(reachable[b], reachable[a]);
swap(reachable[a], reachable[b]);
}
}
else {
col_count[type[a]]--;
if (col_count[type[a]] == 1){
q.push(type[a]);
}
}
return 1;
}
void solve(){
cin >> n >> m >> k;
reset();
for (int i = 1; i <= n; i++){
cin >> type[i];
col_count[type[i]]++;
types[type[i]].push_back(i);
}
for (int i = 1; i <= k; i++){
if (col_count[i] == 1)
q.push(i);
}
for (int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
if (type[a] == type[b]){
Union(a, b);
}
g[a].push_back(b);
g[b].push_back(a);
}
while (!q.empty()){
int t = q.front();
q.pop();
set<int> reps;
map<int, int> nei;
for (auto u: types[t]){
for (auto v: g[u]){
assert(type[v] == type[Find(v)]);
if (type[v] == 0)
reps.insert(Find(v));
else if (type[v] != t){
int a = Find(v);
if (nei.count(type[a])){
Union(nei[type[a]], a);
}
else {
nei[type[a]] = a;
}
}
}
}
int bestid = 0, max_size = nei.size();
for (auto el: reps){
if (reachable[el].size() > max_size){
bestid = el;
max_size = reachable[el].size();
}
}
map<int, int> cur;
if (bestid > 0){
cur = move(reachable[bestid]);
merge_maps(cur, nei);
}
else {
cur = nei;
}
for (auto el: reps){
if (el == bestid)
continue;
merge_maps(cur, reachable[el]);
}
for (auto el: types[t]){
type[el] = 0;
}
for (auto el: reps){
Union(el, types[t][0]);
}
cur.erase(t);
reachable[Find(types[t][0])] = move(cur);
}
bool ok = 1;
for (int i = 1; i <= k; i++)
if (col_count[i] > 1){
ok = 0;
break;
}
if (ok){
cout << "TAK\n";
}
else{
cout << "NIE\n";
}
}
int te = 1;
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> te;
while(te--)
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 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 173 174 175 176 177 178 179 | #include <bits/stdc++.h> #define pii pair<int, int> #define For(i, l, r) for (int i=l;(l<=r?i<=r:i>=r);(l<=r?i++:i--)) #define DEBUG #ifdef DEBUG auto operator<<(auto &o,auto p)->decltype(p.first,o){return o<<'('<<p.first<<", "<<p.second<<')';} auto operator<<(auto &o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto &e:x)o<<(", ")+i<<e,i=0;return o<<'}';} #define LOG(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X); #else #define LOG(x...)(void)0 #endif //#define int long long using namespace std; const int M = 100005; int n, m, k; int rep[M], siz[M], col_count[M], type[M]; vector<int> g[M], types[M]; queue<int> q; map<int, int> reachable[M]; void reset(){ for (int i = 1; i <= k; i++){ col_count[i] = 0; types[i].clear(); reachable[i].clear(); } col_count[0] = 5 * n; for (int i = 0; i <= n; i++){ g[i].clear(); siz[i] = 1; rep[i] = i; } q = {}; } int Find(int a){ if (a == rep[a]) return a; return rep[a] = Find(rep[a]); } int Union(int a, int b); void merge_maps(map<int, int> &a, map<int, int> &b){ for (auto [t, id]: b){ if (type[id] != t) continue; if (a.count(t) && type[a[t]] == t){ Union(a[t], id); } else { a[t] = id; } } b.clear(); } int Union(int a, int b){ assert(type[a] == type[b]); a = Find(a); b = Find(b); if (a == b) return 0; if (siz[a] < siz[b]) swap(a, b); rep[b] = a; siz[a] += siz[b]; if (type[a] == 0){ if (reachable[a].size() >= reachable[b].size()) merge_maps(reachable[a], reachable[b]); else { merge_maps(reachable[b], reachable[a]); swap(reachable[a], reachable[b]); } } else { col_count[type[a]]--; if (col_count[type[a]] == 1){ q.push(type[a]); } } return 1; } void solve(){ cin >> n >> m >> k; reset(); for (int i = 1; i <= n; i++){ cin >> type[i]; col_count[type[i]]++; types[type[i]].push_back(i); } for (int i = 1; i <= k; i++){ if (col_count[i] == 1) q.push(i); } for (int i = 0; i < m; i++){ int a, b; cin >> a >> b; if (type[a] == type[b]){ Union(a, b); } g[a].push_back(b); g[b].push_back(a); } while (!q.empty()){ int t = q.front(); q.pop(); set<int> reps; map<int, int> nei; for (auto u: types[t]){ for (auto v: g[u]){ assert(type[v] == type[Find(v)]); if (type[v] == 0) reps.insert(Find(v)); else if (type[v] != t){ int a = Find(v); if (nei.count(type[a])){ Union(nei[type[a]], a); } else { nei[type[a]] = a; } } } } int bestid = 0, max_size = nei.size(); for (auto el: reps){ if (reachable[el].size() > max_size){ bestid = el; max_size = reachable[el].size(); } } map<int, int> cur; if (bestid > 0){ cur = move(reachable[bestid]); merge_maps(cur, nei); } else { cur = nei; } for (auto el: reps){ if (el == bestid) continue; merge_maps(cur, reachable[el]); } for (auto el: types[t]){ type[el] = 0; } for (auto el: reps){ Union(el, types[t][0]); } cur.erase(t); reachable[Find(types[t][0])] = move(cur); } bool ok = 1; for (int i = 1; i <= k; i++) if (col_count[i] > 1){ ok = 0; break; } if (ok){ cout << "TAK\n"; } else{ cout << "NIE\n"; } } int te = 1; signed main(){ cin.tie(0)->sync_with_stdio(0); cin >> te; while(te--) solve(); } |
English