#include <bits/stdc++.h>
struct DSU{
DSU () {}
DSU (int n){
init(n);
}
std::vector<int> p, siz;
void init(int n){
p.resize(n);
siz.resize(n);
for(int i = 0;i < n;i++){
p[i] = i;
siz[i] = 1;
}
}
int find_par(int u){
return p[u] = (p[u] == u ? u : find_par(p[u]));
}
void unite(int a, int b){
a = find_par(a);
b = find_par(b);
if(a == b) return ;
if(siz[a] < siz[b]) std::swap(a, b);
p[b] = a;
siz[a] += siz[b];
}
};
int main(){
using namespace std;
ios::sync_with_stdio(false), cin.tie(nullptr);
int tt;
cin >> tt;
while(tt--){
int n, m, k;
cin >> n >> m >> k; // { nodes, edges, leaders }
vector<int> who_won(n);
vector<vector<int>> where_won(k);
for(int i = 0;i < n;i++){
cin >> who_won[i], who_won[i]--; // kto wygrał w i-tym mieście
where_won[who_won[i]].push_back(i);
}
DSU dsu2(n);
vector<vector<int>> g(n);
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);
dsu2.unite(a, b);
}
bool ok = true;
for(int i = 0;i < k;i++){
for(int j = 1;j < (int)where_won[i].size();++j){
int a = where_won[i][j];
int b = where_won[i][j - 1];
if(dsu2.find_par(a) != dsu2.find_par(b)){
ok = false;
}
}
}
if(!ok){
cout << "NIE\n";
continue;
}
// we know that every leader stayed in one component as he should
DSU dsu(n);
vector<int> done(n);
for(int i = 0;i < n;i++){
for(int j : g[i]) if(who_won[i] == who_won[j]){
dsu.unite(i, j);
}
}
vector<int> proc;
for(int leader = 0;leader < k;leader++){
if(where_won[leader].empty()) continue;
int node = where_won[leader][0];
if(dsu.siz[dsu.find_par(node)] == (int)where_won[leader].size()){
// this one is done
for(int x : where_won[leader]){
done[x] = 1;
}
proc.push_back(leader);
}
}
if(proc.empty()){
cout << "NIE\n";
continue;
}
vector<vector<int>> collected(k);
while(!proc.empty()){
int leader = proc.back();
proc.pop_back();
// hard part
set<int> gr;
for(int x : where_won[leader]){
for(int y : g[x]){
if(!done[y]){
gr.insert(who_won[y]);
collected[who_won[y]].push_back(y);
}
}
}
for(int x : gr){
if(collected[x].size() > 1){
for(int j = 1;j < (int)collected[x].size();j++){
int a = collected[x][j];
int b = collected[x][j - 1];
dsu.unite(a, b);
}
if(dsu.siz[dsu.find_par(collected[x][0])] == (int)where_won[x].size()){
// we are done with this one.
for(int x : where_won[x]){
done[x]=1;
}
proc.push_back(x);
}
}
collected[x].clear();
}
}
for(int i = 0;i < n;i++){
ok &= (done[i] == true);
}
cout << (ok ? "TAK\n" : "NIE\n");
}
return 0;
}
// graf nie musi być spójny
// start from completed components
// if they are full (all nodes that needed)
// set it as done and add all the new connections that you can have.
// a bit tricky but doable.
// how to implement it as nicely as possible
// DSU ?
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 | #include <bits/stdc++.h> struct DSU{ DSU () {} DSU (int n){ init(n); } std::vector<int> p, siz; void init(int n){ p.resize(n); siz.resize(n); for(int i = 0;i < n;i++){ p[i] = i; siz[i] = 1; } } int find_par(int u){ return p[u] = (p[u] == u ? u : find_par(p[u])); } void unite(int a, int b){ a = find_par(a); b = find_par(b); if(a == b) return ; if(siz[a] < siz[b]) std::swap(a, b); p[b] = a; siz[a] += siz[b]; } }; int main(){ using namespace std; ios::sync_with_stdio(false), cin.tie(nullptr); int tt; cin >> tt; while(tt--){ int n, m, k; cin >> n >> m >> k; // { nodes, edges, leaders } vector<int> who_won(n); vector<vector<int>> where_won(k); for(int i = 0;i < n;i++){ cin >> who_won[i], who_won[i]--; // kto wygrał w i-tym mieście where_won[who_won[i]].push_back(i); } DSU dsu2(n); vector<vector<int>> g(n); 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); dsu2.unite(a, b); } bool ok = true; for(int i = 0;i < k;i++){ for(int j = 1;j < (int)where_won[i].size();++j){ int a = where_won[i][j]; int b = where_won[i][j - 1]; if(dsu2.find_par(a) != dsu2.find_par(b)){ ok = false; } } } if(!ok){ cout << "NIE\n"; continue; } // we know that every leader stayed in one component as he should DSU dsu(n); vector<int> done(n); for(int i = 0;i < n;i++){ for(int j : g[i]) if(who_won[i] == who_won[j]){ dsu.unite(i, j); } } vector<int> proc; for(int leader = 0;leader < k;leader++){ if(where_won[leader].empty()) continue; int node = where_won[leader][0]; if(dsu.siz[dsu.find_par(node)] == (int)where_won[leader].size()){ // this one is done for(int x : where_won[leader]){ done[x] = 1; } proc.push_back(leader); } } if(proc.empty()){ cout << "NIE\n"; continue; } vector<vector<int>> collected(k); while(!proc.empty()){ int leader = proc.back(); proc.pop_back(); // hard part set<int> gr; for(int x : where_won[leader]){ for(int y : g[x]){ if(!done[y]){ gr.insert(who_won[y]); collected[who_won[y]].push_back(y); } } } for(int x : gr){ if(collected[x].size() > 1){ for(int j = 1;j < (int)collected[x].size();j++){ int a = collected[x][j]; int b = collected[x][j - 1]; dsu.unite(a, b); } if(dsu.siz[dsu.find_par(collected[x][0])] == (int)where_won[x].size()){ // we are done with this one. for(int x : where_won[x]){ done[x]=1; } proc.push_back(x); } } collected[x].clear(); } } for(int i = 0;i < n;i++){ ok &= (done[i] == true); } cout << (ok ? "TAK\n" : "NIE\n"); } return 0; } // graf nie musi być spójny // start from completed components // if they are full (all nodes that needed) // set it as done and add all the new connections that you can have. // a bit tricky but doable. // how to implement it as nicely as possible // DSU ? |
English