#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
ll Find(vll &groups, ll v) {
if (groups[v] == v) return v;
return groups[v] = Find(groups, groups[v]);
}
void Union(vll &groups, ll a, ll b) {
ll fa = Find(groups, a);
ll fb = Find(groups, b);
if (fa < fb) groups[fb] = fa;
else groups[fa] = fb;
}
void uncolor(vector<vll> &graph, int start, vll &groups, vll &colors, vll &groupCnt, queue<ll> &cohesion, vector<bool> &uncolored) {
vector<bool> visited(graph.size(), false);
vll other(groupCnt.size(), -1);
queue<ll> q;
q.push(start);
visited[start] = true;
int cnt = 0;
while(!q.empty())
{
int v = q.front();
q.pop();
cnt++;
for (int node: graph[v]) {
if (!visited[node] && (colors[node] == colors[start] || uncolored[colors[node] - 1])) {
q.push(node);
visited[node] = true;
} else if (colors[node] != colors[start]) {
if (other[colors[node]-1] == -1) other[colors[node]-1] = node;
else {
if (Find(groups, other[colors[node]-1]) != Find(groups, node)) {
Union(groups, other[colors[node]-1], node);
groupCnt[colors[node]-1]--;
graph[node].pb(other[colors[node]-1]);
graph[other[colors[node]-1]].pb(node);
if (groupCnt[colors[node]-1] == 1) cohesion.push(node);
}
}
}
}
}
}
bool checkCohesion(vector<vll> &graph, int start, int total, vll &colors, vll &visited, int group) {
queue<ll> q;
q.push(start);
visited[start] = group;
int cnt = 0;
while(!q.empty())
{
int v = q.front();
q.pop();
cnt++;
for (int node: graph[v]) {
if (visited[node] == -1 && colors[node] == colors[start]) {
q.push(node);
visited[node] = group;
}
}
}
return cnt == total;
}
bool solve()
{
int n, m, k;
cin >> n >> m >> k;
vector<vll> graph(n);
vll col(n), cnts(k, 0);
vector<bool> uncolored(k, 0);
for (int i = 0; i < n; ++i) {
cin >> col[i];
cnts[col[i] - 1]++;
}
ll a, b;
for (int i = 0; i < m; ++i) {
cin >> a >> b;
graph[a-1].pb(b-1);
graph[b-1].pb(a-1);
}
queue<ll> cohesion;
vector<ll> visited(graph.size(), -1);
vector<ll> groupsCnt(k, 0);
for (int i = 0; i < n; ++i) {
if (visited[i] == -1) {
}
if (visited[i] == -1) {
if (checkCohesion(graph, i, cnts[col[i]-1], col, visited, i)) {
cohesion.push(i);
}
groupsCnt[col[i]-1]++;
}
}
while (!cohesion.empty()) {
int start = cohesion.front();
cohesion.pop();
uncolor(graph, start, visited, col, groupsCnt, cohesion, uncolored);
uncolored[col[start]-1] = true;
}
for (int i = 0; i < n; ++i) {
if (!uncolored[col[i]-1]) return false;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
cout << (solve() ? "TAK" : "NIE") << endl;
}
}
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 | #include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; typedef vector<ll> vll; ll Find(vll &groups, ll v) { if (groups[v] == v) return v; return groups[v] = Find(groups, groups[v]); } void Union(vll &groups, ll a, ll b) { ll fa = Find(groups, a); ll fb = Find(groups, b); if (fa < fb) groups[fb] = fa; else groups[fa] = fb; } void uncolor(vector<vll> &graph, int start, vll &groups, vll &colors, vll &groupCnt, queue<ll> &cohesion, vector<bool> &uncolored) { vector<bool> visited(graph.size(), false); vll other(groupCnt.size(), -1); queue<ll> q; q.push(start); visited[start] = true; int cnt = 0; while(!q.empty()) { int v = q.front(); q.pop(); cnt++; for (int node: graph[v]) { if (!visited[node] && (colors[node] == colors[start] || uncolored[colors[node] - 1])) { q.push(node); visited[node] = true; } else if (colors[node] != colors[start]) { if (other[colors[node]-1] == -1) other[colors[node]-1] = node; else { if (Find(groups, other[colors[node]-1]) != Find(groups, node)) { Union(groups, other[colors[node]-1], node); groupCnt[colors[node]-1]--; graph[node].pb(other[colors[node]-1]); graph[other[colors[node]-1]].pb(node); if (groupCnt[colors[node]-1] == 1) cohesion.push(node); } } } } } } bool checkCohesion(vector<vll> &graph, int start, int total, vll &colors, vll &visited, int group) { queue<ll> q; q.push(start); visited[start] = group; int cnt = 0; while(!q.empty()) { int v = q.front(); q.pop(); cnt++; for (int node: graph[v]) { if (visited[node] == -1 && colors[node] == colors[start]) { q.push(node); visited[node] = group; } } } return cnt == total; } bool solve() { int n, m, k; cin >> n >> m >> k; vector<vll> graph(n); vll col(n), cnts(k, 0); vector<bool> uncolored(k, 0); for (int i = 0; i < n; ++i) { cin >> col[i]; cnts[col[i] - 1]++; } ll a, b; for (int i = 0; i < m; ++i) { cin >> a >> b; graph[a-1].pb(b-1); graph[b-1].pb(a-1); } queue<ll> cohesion; vector<ll> visited(graph.size(), -1); vector<ll> groupsCnt(k, 0); for (int i = 0; i < n; ++i) { if (visited[i] == -1) { } if (visited[i] == -1) { if (checkCohesion(graph, i, cnts[col[i]-1], col, visited, i)) { cohesion.push(i); } groupsCnt[col[i]-1]++; } } while (!cohesion.empty()) { int start = cohesion.front(); cohesion.pop(); uncolor(graph, start, visited, col, groupsCnt, cohesion, uncolored); uncolored[col[start]-1] = true; } for (int i = 0; i < n; ++i) { if (!uncolored[col[i]-1]) return false; } return true; } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { cout << (solve() ? "TAK" : "NIE") << endl; } } |
English