#include <bits/stdc++.h>
using namespace std;
struct node {
int party;
int index;
int parent; //parent index
int size; //for dsu
vector<pair<int, int>> neigh; //{party, index}
node(int party_, int index_) {
party = party_;
index = index_;
parent = index_; //itself
size = 1;
}
};
int nodeFind(vector<node> &party, int cur) {
if(party[cur].parent == cur)
return cur;
int parent = nodeFind(party, party[cur].parent);
party[cur].parent = parent;
return parent;
}
void nodeUnion(vector<node> &party, int a, int b) {
a = nodeFind(party, a);
b = nodeFind(party, b);
if(a == b)
return;
if(party[a].size < party[b].size)
swap(a, b);
party[b].parent = a;
party[a].size += party[b].size;
}
vector<vector<node>> input() {
int n, m, k;
cin >> n >> m >> k;
vector<int> cities(n);
for(auto &a : cities)
cin >> a;
int count = 0; //number of parties found
unordered_map<int, int> translate;
for(auto a : cities) {
if(translate.count(a) == 0) {
translate[a] = count++;
}
}
vector<vector<node>> graph(count);
vector<int> indexes(n);
for(int i=0; i<n; i++) {
int translated = translate[cities[i]];
int index = graph[translated].size();
graph[translated].emplace_back(translated, index);
indexes[i] = index;
}
for(int j=0; j<m; j++) { //edges
int a, b;
cin >> a >> b;
pair<int, int> one = {translate[cities[a-1]], indexes[a-1]};
pair<int, int> two = {translate[cities[b-1]], indexes[b-1]};
graph[one.first][one.second].neigh.emplace_back(two.first, two.second);
graph[two.first][two.second].neigh.emplace_back(one.first, one.second);
if(one.first == two.first) //same part -> connect
nodeUnion(graph[one.first], one.second, two.second);
}
return graph;
}
typedef pair<unordered_map<int, int>, int> mapDsu; //{party -> index, parent}
int mapFind(vector<mapDsu> &maps, int cur) {
if(maps[cur].second == cur)
return cur;
int parent = mapFind(maps, maps[cur].second);
maps[cur].second = parent;
return parent;
}
unordered_set<int> mapUnion(vector<vector<node>> &graph, vector<mapDsu> &maps, int a, int b) {
a = mapFind(maps, a);
b = mapFind(maps, b);
if(a == b)
return {};
if(maps[a].first.size() < maps[b].first.size())
swap(a, b);
maps[b].second = a;
unordered_set<int> alteredParties;
for(auto [party, index] : maps[b].first) { //copy contents of the smaller map to the bigger one
//if possible - connect nodes
if(maps[a].first.count(party) != 0 && index != maps[a].first[party]) {
nodeUnion(graph[party], index, maps[a].first[party]);
alteredParties.insert(party);
}
maps[a].first[party] = index;
}
maps[b].first.clear(); //erase the smaller map to save space
return alteredParties;
}
bool checkSuccess(vector<bool> &legal) {
for(auto a : legal)
if(!a)
return false;
return true;
}
void runAll() {
vector<vector<node>> graph = input(); //graph[party][index]
vector<bool> legal(graph.size(), false);
queue<int> toCheck;
vector<mapDsu> maps(graph.size()); //{party -> index, parent}, like dsu
for(int i=0; i<graph.size(); i++)
maps[i].second = i;
for(int i=0; i<graph.size(); i++) { //already legal -> add to queue
if(graph[i][nodeFind(graph[i], 0)].size == graph[i].size()) { //all nodes connected
toCheck.push(i);
legal[i] = true;
}
}
while(!toCheck.empty()) { //check all currently legal
int cur = toCheck.front();
toCheck.pop();
unordered_set<int> alteredParties;
for(auto &member : graph[cur]) //connect all possible maps first
for(auto [party, index] : member.neigh)
if(legal[party]) //connect (just in case)
alteredParties.merge(mapUnion(graph, maps, cur, party));
unordered_map<int, int> &around = maps[mapFind(maps, cur)].first; //current, updated map
for(auto &member : graph[cur]) {
for(auto [party, index] : member.neigh) {
if(legal[party])
continue;
else if(around.count(party) == 0)
around[party] = index;
else {
nodeUnion(graph[party], around[party], index);
alteredParties.insert(party);
}
}
}
for(auto party : alteredParties) {
if(graph[party][nodeFind(graph[party], 0)].size == graph[party].size()) {
legal[party] = true;
toCheck.push(party);
}
}
}
if(checkSuccess(legal))
cout << "TAK\n";
else
cout << "NIE\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while(t--)
runAll();
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include <bits/stdc++.h> using namespace std; struct node { int party; int index; int parent; //parent index int size; //for dsu vector<pair<int, int>> neigh; //{party, index} node(int party_, int index_) { party = party_; index = index_; parent = index_; //itself size = 1; } }; int nodeFind(vector<node> &party, int cur) { if(party[cur].parent == cur) return cur; int parent = nodeFind(party, party[cur].parent); party[cur].parent = parent; return parent; } void nodeUnion(vector<node> &party, int a, int b) { a = nodeFind(party, a); b = nodeFind(party, b); if(a == b) return; if(party[a].size < party[b].size) swap(a, b); party[b].parent = a; party[a].size += party[b].size; } vector<vector<node>> input() { int n, m, k; cin >> n >> m >> k; vector<int> cities(n); for(auto &a : cities) cin >> a; int count = 0; //number of parties found unordered_map<int, int> translate; for(auto a : cities) { if(translate.count(a) == 0) { translate[a] = count++; } } vector<vector<node>> graph(count); vector<int> indexes(n); for(int i=0; i<n; i++) { int translated = translate[cities[i]]; int index = graph[translated].size(); graph[translated].emplace_back(translated, index); indexes[i] = index; } for(int j=0; j<m; j++) { //edges int a, b; cin >> a >> b; pair<int, int> one = {translate[cities[a-1]], indexes[a-1]}; pair<int, int> two = {translate[cities[b-1]], indexes[b-1]}; graph[one.first][one.second].neigh.emplace_back(two.first, two.second); graph[two.first][two.second].neigh.emplace_back(one.first, one.second); if(one.first == two.first) //same part -> connect nodeUnion(graph[one.first], one.second, two.second); } return graph; } typedef pair<unordered_map<int, int>, int> mapDsu; //{party -> index, parent} int mapFind(vector<mapDsu> &maps, int cur) { if(maps[cur].second == cur) return cur; int parent = mapFind(maps, maps[cur].second); maps[cur].second = parent; return parent; } unordered_set<int> mapUnion(vector<vector<node>> &graph, vector<mapDsu> &maps, int a, int b) { a = mapFind(maps, a); b = mapFind(maps, b); if(a == b) return {}; if(maps[a].first.size() < maps[b].first.size()) swap(a, b); maps[b].second = a; unordered_set<int> alteredParties; for(auto [party, index] : maps[b].first) { //copy contents of the smaller map to the bigger one //if possible - connect nodes if(maps[a].first.count(party) != 0 && index != maps[a].first[party]) { nodeUnion(graph[party], index, maps[a].first[party]); alteredParties.insert(party); } maps[a].first[party] = index; } maps[b].first.clear(); //erase the smaller map to save space return alteredParties; } bool checkSuccess(vector<bool> &legal) { for(auto a : legal) if(!a) return false; return true; } void runAll() { vector<vector<node>> graph = input(); //graph[party][index] vector<bool> legal(graph.size(), false); queue<int> toCheck; vector<mapDsu> maps(graph.size()); //{party -> index, parent}, like dsu for(int i=0; i<graph.size(); i++) maps[i].second = i; for(int i=0; i<graph.size(); i++) { //already legal -> add to queue if(graph[i][nodeFind(graph[i], 0)].size == graph[i].size()) { //all nodes connected toCheck.push(i); legal[i] = true; } } while(!toCheck.empty()) { //check all currently legal int cur = toCheck.front(); toCheck.pop(); unordered_set<int> alteredParties; for(auto &member : graph[cur]) //connect all possible maps first for(auto [party, index] : member.neigh) if(legal[party]) //connect (just in case) alteredParties.merge(mapUnion(graph, maps, cur, party)); unordered_map<int, int> &around = maps[mapFind(maps, cur)].first; //current, updated map for(auto &member : graph[cur]) { for(auto [party, index] : member.neigh) { if(legal[party]) continue; else if(around.count(party) == 0) around[party] = index; else { nodeUnion(graph[party], around[party], index); alteredParties.insert(party); } } } for(auto party : alteredParties) { if(graph[party][nodeFind(graph[party], 0)].size == graph[party].size()) { legal[party] = true; toCheck.push(party); } } } if(checkSuccess(legal)) cout << "TAK\n"; else cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while(t--) runAll(); return 0; } |
English