#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 5;
struct gfg_custom_hash {
static uint64_t splitmix64(uint64_t key)
{
key += 0x9e3779b97f4a7c15;
key = (key ^ (key >> 30)) * 0xbf58476d1ce4e5b9;
key = (key ^ (key >> 27)) * 0x94d049bb133111eb;
return key ^ (key >> 31);
}
// function to make the hash function non-deterministic
size_t operator()(uint64_t x) const
{
static const uint64_t RANDOM
= chrono::steady_clock::now()
.time_since_epoch()
.count();
return splitmix64(x + RANDOM);
}
};
int cnt, n, x, y, m, k, a[N], t, cnum[N], s[N], p[N];
vector<int> g[N], color[N];
bool seen[N], check, wp[N], widziane[N];
unordered_map<int, int, gfg_custom_hash> hmap[N];
unordered_set<int, gfg_custom_hash> hs;
int dsufind(int A) {
while(p[A] != A) return p[A] = dsufind(p[A]);
return A;
}
bool in_union(const int& A, const int& B) {
return dsufind(A) == dsufind(B);
}
void dsumerge(int A, int B) {
if(in_union(A, B)) return;
A = dsufind(A);
B = dsufind(B);
if(s[A] >= s[B]) {
s[A] += s[B];
p[B] = A;
} else {
s[B] += s[A];
p[A] = B;
}
}
int dfs1(const int& v) {
int ss = 1;
seen[v] = 1;
for(const int& ch : g[v]) {
if(!seen[ch] && a[v] == a[ch]) {
dsumerge(v, ch);
ss += dfs1(ch);
}
}
return ss;
}
void dfs2(int v, int num) {
seen[v] = 1;
cnum[a[v]] = num;
for (const int& ch : g[v]) {
if(cnum[ch] != -1 && cnum[ch] != num) check = 1;
if(!seen[ch]) {
dfs2(ch, num);
}
}
}
bool solve() {
cin >> n >> m >> k;
for(int i = 1; i <= n; ++i) { g[i].clear(); seen[i] = 0; p[i] = i; s[i] = 1; cnum[i] = -1; }
for(int i = 1; i <= k; ++i) { color[i].clear(); wp[i] = 0; hmap[i].clear(); widziane[i] = 0; }
for(int i = 1; i <= n; ++i) { cin >> a[i]; color[a[i]].push_back(i); }
for(int i = 0; i < m; ++i) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
cnt = 0;
queue<int> q;
for(int v = 1; v <= n; ++v) {
if(!seen[v]) {
if(dfs1(v) == color[a[v]].size()) {
++cnt;
q.push(a[v]);
widziane[a[v]] = 1;
}
}
}
if(!cnt) return 0;
int num = 1, check = 0;
for(int i = 1; i <= n; ++i) seen[i] = 0;
for(int i = 1; i <= n; ++i) {
if(!seen[i]) { dfs2(i, num); ++num; }
if(check) return 0;
}
cnt = 0;
// there exists at least one color which is connected + for each color all of its cities can be reached from each other.
while(!q.empty()) {
int col = q.front(); q.pop();
if(wp[col]) continue;
cnt += color[col].size();
wp[col] = 1;
int ind = col;
hs.clear();
for(const int& v : color[col]) {
for(const int& u : g[v]) { //O(m)
if(a[u] == col) continue;
if(wp[a[u]]) {
if(hs.find(dsufind(u)) != hs.end()) continue;
hs.insert(dsufind(u));
if(hmap[ind].size() < hmap[a[dsufind(u)]].size()) ind = a[u];
} else {
auto it = hmap[col].find(a[u]);
if(it == hmap[col].end()) hmap[col][a[u]] = u;
else {
dsumerge(u, it->second);
if(s[dsufind(u)] == color[a[u]].size() && !widziane[a[u]]) { q.push(a[u]); widziane[a[u]] = 1; }
it->second = dsufind(u);
}
}
}
}
if(hmap[ind].size() < hmap[col].size()) ind = col;
hs.insert(dsufind(color[col][0]));
for(const int& c : hs) {
if(a[c] == ind) continue;
dsumerge(c, color[ind][0]); // mergujemy te wp ze soba co potem stanowia calo jedno wp
for(auto[cc, rep] : hmap[a[c]]) {
auto it = hmap[ind].find(cc);
if(it == hmap[ind].end()) hmap[ind][cc] = rep;
else {
dsumerge(rep, it->second);
if(s[dsufind(rep)] == color[a[rep]].size() && !widziane[a[rep]]) { q.push(a[rep]); widziane[a[rep]] = 1; }
it->second = dsufind(rep);
}
}
}
if(a[dsufind(color[col][0])] != ind) swap(hmap[ind], hmap[a[dsufind(color[col][0])]]);
}
return cnt == n;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> t;
while(t--) cout << (solve() ? "TAK\n" : "NIE\n");
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 160 161 162 163 164 | #include <bits/stdc++.h> using namespace std; constexpr int N = 1e5 + 5; struct gfg_custom_hash { static uint64_t splitmix64(uint64_t key) { key += 0x9e3779b97f4a7c15; key = (key ^ (key >> 30)) * 0xbf58476d1ce4e5b9; key = (key ^ (key >> 27)) * 0x94d049bb133111eb; return key ^ (key >> 31); } // function to make the hash function non-deterministic size_t operator()(uint64_t x) const { static const uint64_t RANDOM = chrono::steady_clock::now() .time_since_epoch() .count(); return splitmix64(x + RANDOM); } }; int cnt, n, x, y, m, k, a[N], t, cnum[N], s[N], p[N]; vector<int> g[N], color[N]; bool seen[N], check, wp[N], widziane[N]; unordered_map<int, int, gfg_custom_hash> hmap[N]; unordered_set<int, gfg_custom_hash> hs; int dsufind(int A) { while(p[A] != A) return p[A] = dsufind(p[A]); return A; } bool in_union(const int& A, const int& B) { return dsufind(A) == dsufind(B); } void dsumerge(int A, int B) { if(in_union(A, B)) return; A = dsufind(A); B = dsufind(B); if(s[A] >= s[B]) { s[A] += s[B]; p[B] = A; } else { s[B] += s[A]; p[A] = B; } } int dfs1(const int& v) { int ss = 1; seen[v] = 1; for(const int& ch : g[v]) { if(!seen[ch] && a[v] == a[ch]) { dsumerge(v, ch); ss += dfs1(ch); } } return ss; } void dfs2(int v, int num) { seen[v] = 1; cnum[a[v]] = num; for (const int& ch : g[v]) { if(cnum[ch] != -1 && cnum[ch] != num) check = 1; if(!seen[ch]) { dfs2(ch, num); } } } bool solve() { cin >> n >> m >> k; for(int i = 1; i <= n; ++i) { g[i].clear(); seen[i] = 0; p[i] = i; s[i] = 1; cnum[i] = -1; } for(int i = 1; i <= k; ++i) { color[i].clear(); wp[i] = 0; hmap[i].clear(); widziane[i] = 0; } for(int i = 1; i <= n; ++i) { cin >> a[i]; color[a[i]].push_back(i); } for(int i = 0; i < m; ++i) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } cnt = 0; queue<int> q; for(int v = 1; v <= n; ++v) { if(!seen[v]) { if(dfs1(v) == color[a[v]].size()) { ++cnt; q.push(a[v]); widziane[a[v]] = 1; } } } if(!cnt) return 0; int num = 1, check = 0; for(int i = 1; i <= n; ++i) seen[i] = 0; for(int i = 1; i <= n; ++i) { if(!seen[i]) { dfs2(i, num); ++num; } if(check) return 0; } cnt = 0; // there exists at least one color which is connected + for each color all of its cities can be reached from each other. while(!q.empty()) { int col = q.front(); q.pop(); if(wp[col]) continue; cnt += color[col].size(); wp[col] = 1; int ind = col; hs.clear(); for(const int& v : color[col]) { for(const int& u : g[v]) { //O(m) if(a[u] == col) continue; if(wp[a[u]]) { if(hs.find(dsufind(u)) != hs.end()) continue; hs.insert(dsufind(u)); if(hmap[ind].size() < hmap[a[dsufind(u)]].size()) ind = a[u]; } else { auto it = hmap[col].find(a[u]); if(it == hmap[col].end()) hmap[col][a[u]] = u; else { dsumerge(u, it->second); if(s[dsufind(u)] == color[a[u]].size() && !widziane[a[u]]) { q.push(a[u]); widziane[a[u]] = 1; } it->second = dsufind(u); } } } } if(hmap[ind].size() < hmap[col].size()) ind = col; hs.insert(dsufind(color[col][0])); for(const int& c : hs) { if(a[c] == ind) continue; dsumerge(c, color[ind][0]); // mergujemy te wp ze soba co potem stanowia calo jedno wp for(auto[cc, rep] : hmap[a[c]]) { auto it = hmap[ind].find(cc); if(it == hmap[ind].end()) hmap[ind][cc] = rep; else { dsumerge(rep, it->second); if(s[dsufind(rep)] == color[a[rep]].size() && !widziane[a[rep]]) { q.push(a[rep]); widziane[a[rep]] = 1; } it->second = dsufind(rep); } } } if(a[dsufind(color[col][0])] != ind) swap(hmap[ind], hmap[a[dsufind(color[col][0])]]); } return cnt == n; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> t; while(t--) cout << (solve() ? "TAK\n" : "NIE\n"); return 0; } |
English