#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
//#define DEBUG
vector<int> G[100001], wierzcholki_partii[100001];
queue<int> connected;
int partitions[100001];
struct Set {
Set *parent;
int r = 0;
int partia;
int v;
Set(int partia, int v) {
this->partia = partia;
this->v = v;
parent = 0;
}
};
struct Wormhole {
Wormhole *parent = 0;
int r = 0;
map<int, Set*> node_partii;
};
Set* Find(Set *x) {
#ifdef DEBUG
cout << "FIND " << x->v << endl;
#endif // DEBUG
if (x->parent == 0) {
#ifdef DEBUG
cout << "ITS A ROOT" << endl;
#endif // DEBUG
return x;
}
x->parent = Find(x->parent);
return x->parent;
}
Wormhole* Find(Wormhole *x) {
if (x->parent == 0) {
return x;
}
x->parent = Find(x->parent);
return x->parent;
}
void Union(Set *a, Set *b) {
Set *aRoot = Find(a);
Set *bRoot = Find(b);
if (aRoot == bRoot) {
return;
}
partitions[aRoot->partia]--;
if (partitions[aRoot->partia] == 1) {
connected.push(aRoot->partia);
}
if (aRoot->r > bRoot->r) {
bRoot->parent = aRoot;
} else if (bRoot->r > aRoot->r) {
aRoot->parent = bRoot;
} else {
bRoot->parent = aRoot;
aRoot->r = aRoot->r + 1;
}
}
void Union(Wormhole *a, Wormhole *b) {
Wormhole *aRoot = Find(a);
Wormhole *bRoot = Find(b);
if (aRoot == bRoot) {
return;
}
if (aRoot->r > bRoot->r) {
bRoot->parent = aRoot;
} else if (bRoot->r > aRoot->r) {
aRoot->parent = bRoot;
swap(aRoot, bRoot);
} else {
bRoot->parent = aRoot;
aRoot->r = aRoot->r + 1;
}
if (aRoot->node_partii.size() < bRoot->node_partii.size()) {
swap(aRoot->node_partii, bRoot->node_partii);
}
for (auto it = bRoot->node_partii.begin(); it != bRoot->node_partii.end(); it++) {
if (aRoot->node_partii.find(it->first) != aRoot->node_partii.end()) {
Union(it->second, aRoot->node_partii[it->first]);
} else {
aRoot->node_partii[it->first] = it->second;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
int t, V, E, k, P[100001], a, b;
Set *node[100001];
Wormhole *W[100001];
vector<Wormhole*> wormholesForDelete;
cin >> t;
while (t--) {
cin >> V >> E >> k;
for (int i = 1; i <= k; i++) {
partitions[i] = 0;
}
for (int i = 1; i <= V; i++) {
cin >> P[i];
node[i] = new Set(P[i], i);
W[i] = 0;
partitions[P[i]]++;
wierzcholki_partii[P[i]].push_back(i);
}
#ifdef DEBUG
cout << "PARTIE WCZYTANE" << endl;
#endif // DEBUG
for (int i = 0; i < E; i++) {
cin >> a >> b;
#ifdef DEBUG
cout << a << " " << b << endl;
#endif // DEBUG
G[a].push_back(b);
G[b].push_back(a);
if (P[a] == P[b] && Find(node[a]) != Find(node[b])) {
#ifdef DEBUG
cout << a << " I " << b << " MAJA TE SAME PARTIE" << endl;
#endif // DEBUG
Union(node[a], node[b]);
}
}
#ifdef DEBUG
cout << "KRAWEDZIE WCZYTANE" << endl;
#endif // DEBUG
for (int i = 1; i <= k; i++) {
#ifdef DEBUG
cout << "PARTITIONS[" << i << "]: " << partitions[i] << endl;
#endif // DEBUG
if (partitions[i] == 1) {
connected.push(i);
}
}
while (!connected.empty()) {
int x = connected.front();
connected.pop();
#ifdef DEBUG
cout << "PARTIA " << x << " JEST CACY!" << endl;
#endif // DEBUG
Wormhole *w = new Wormhole();
wormholesForDelete.push_back(w);
for (int i = 0; i < wierzcholki_partii[x].size(); i++) {
int a = wierzcholki_partii[x][i];
W[a] = w;
for (int j = 0; j < G[a].size(); j++) {
int b = G[a][j];
int partia = P[b];
if (W[b] != 0) {
Union(W[b], w);
w = Find(w);
continue;
}
if (w->node_partii[partia] == 0) {
w->node_partii[partia] = node[b];
} else {
Union(w->node_partii[partia], node[b]);
}
}
}
}
bool possible = true;
for (int i = 1; i <= k; i++) {
if (partitions[i] > 1) {
possible = false;
}
}
if (possible) {
cout << "TAK" << endl;
} else {
cout << "NIE" << endl;
}
for (int i = 1; i <= V; i++) {
G[i].clear();
W[i] = 0;
#ifdef DEBUG
cout << "DELETING NODE " << i << " " << &node[i] << endl;
#endif // DEBUG
delete node[i];
#ifdef DEBUG
cout << "DELETING DONE " << endl;
#endif // DEBUG
}
for (int i = 0; i < wormholesForDelete.size(); i++) {
delete wormholesForDelete[i];
}
wormholesForDelete.clear();
for (int i = 1; i <= k; i++) {
wierzcholki_partii[i].clear();
}
}
}
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | #include <iostream> #include <vector> #include <queue> #include <map> using namespace std; //#define DEBUG vector<int> G[100001], wierzcholki_partii[100001]; queue<int> connected; int partitions[100001]; struct Set { Set *parent; int r = 0; int partia; int v; Set(int partia, int v) { this->partia = partia; this->v = v; parent = 0; } }; struct Wormhole { Wormhole *parent = 0; int r = 0; map<int, Set*> node_partii; }; Set* Find(Set *x) { #ifdef DEBUG cout << "FIND " << x->v << endl; #endif // DEBUG if (x->parent == 0) { #ifdef DEBUG cout << "ITS A ROOT" << endl; #endif // DEBUG return x; } x->parent = Find(x->parent); return x->parent; } Wormhole* Find(Wormhole *x) { if (x->parent == 0) { return x; } x->parent = Find(x->parent); return x->parent; } void Union(Set *a, Set *b) { Set *aRoot = Find(a); Set *bRoot = Find(b); if (aRoot == bRoot) { return; } partitions[aRoot->partia]--; if (partitions[aRoot->partia] == 1) { connected.push(aRoot->partia); } if (aRoot->r > bRoot->r) { bRoot->parent = aRoot; } else if (bRoot->r > aRoot->r) { aRoot->parent = bRoot; } else { bRoot->parent = aRoot; aRoot->r = aRoot->r + 1; } } void Union(Wormhole *a, Wormhole *b) { Wormhole *aRoot = Find(a); Wormhole *bRoot = Find(b); if (aRoot == bRoot) { return; } if (aRoot->r > bRoot->r) { bRoot->parent = aRoot; } else if (bRoot->r > aRoot->r) { aRoot->parent = bRoot; swap(aRoot, bRoot); } else { bRoot->parent = aRoot; aRoot->r = aRoot->r + 1; } if (aRoot->node_partii.size() < bRoot->node_partii.size()) { swap(aRoot->node_partii, bRoot->node_partii); } for (auto it = bRoot->node_partii.begin(); it != bRoot->node_partii.end(); it++) { if (aRoot->node_partii.find(it->first) != aRoot->node_partii.end()) { Union(it->second, aRoot->node_partii[it->first]); } else { aRoot->node_partii[it->first] = it->second; } } } int main() { ios_base::sync_with_stdio(0); int t, V, E, k, P[100001], a, b; Set *node[100001]; Wormhole *W[100001]; vector<Wormhole*> wormholesForDelete; cin >> t; while (t--) { cin >> V >> E >> k; for (int i = 1; i <= k; i++) { partitions[i] = 0; } for (int i = 1; i <= V; i++) { cin >> P[i]; node[i] = new Set(P[i], i); W[i] = 0; partitions[P[i]]++; wierzcholki_partii[P[i]].push_back(i); } #ifdef DEBUG cout << "PARTIE WCZYTANE" << endl; #endif // DEBUG for (int i = 0; i < E; i++) { cin >> a >> b; #ifdef DEBUG cout << a << " " << b << endl; #endif // DEBUG G[a].push_back(b); G[b].push_back(a); if (P[a] == P[b] && Find(node[a]) != Find(node[b])) { #ifdef DEBUG cout << a << " I " << b << " MAJA TE SAME PARTIE" << endl; #endif // DEBUG Union(node[a], node[b]); } } #ifdef DEBUG cout << "KRAWEDZIE WCZYTANE" << endl; #endif // DEBUG for (int i = 1; i <= k; i++) { #ifdef DEBUG cout << "PARTITIONS[" << i << "]: " << partitions[i] << endl; #endif // DEBUG if (partitions[i] == 1) { connected.push(i); } } while (!connected.empty()) { int x = connected.front(); connected.pop(); #ifdef DEBUG cout << "PARTIA " << x << " JEST CACY!" << endl; #endif // DEBUG Wormhole *w = new Wormhole(); wormholesForDelete.push_back(w); for (int i = 0; i < wierzcholki_partii[x].size(); i++) { int a = wierzcholki_partii[x][i]; W[a] = w; for (int j = 0; j < G[a].size(); j++) { int b = G[a][j]; int partia = P[b]; if (W[b] != 0) { Union(W[b], w); w = Find(w); continue; } if (w->node_partii[partia] == 0) { w->node_partii[partia] = node[b]; } else { Union(w->node_partii[partia], node[b]); } } } } bool possible = true; for (int i = 1; i <= k; i++) { if (partitions[i] > 1) { possible = false; } } if (possible) { cout << "TAK" << endl; } else { cout << "NIE" << endl; } for (int i = 1; i <= V; i++) { G[i].clear(); W[i] = 0; #ifdef DEBUG cout << "DELETING NODE " << i << " " << &node[i] << endl; #endif // DEBUG delete node[i]; #ifdef DEBUG cout << "DELETING DONE " << endl; #endif // DEBUG } for (int i = 0; i < wormholesForDelete.size(); i++) { delete wormholesForDelete[i]; } wormholesForDelete.clear(); for (int i = 1; i <= k; i++) { wierzcholki_partii[i].clear(); } } } |
English