#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)
#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef unsigned int uint;
const int INF = 1e9 + 9;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "vector[";
for (const auto &e : v) {
os << e << ", ";
}
os << "]";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &v) {
os << "unordered_set[";
for (const auto &e : v) {
os << e << ", ";
}
os << "]";
return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const unordered_map<K, V> &m) {
os << "unordered_map[";
for (const auto &[k, v] : m) {
os << "(" << k << "," << v << "), ";
}
os << "]";
return os;
}
///// BEGIN FIND_UNION WITH PATH COMPRESSION AND RANKS
struct FindUnion {
int n;
vector<int> parent; // if >= 0 then it's parent node, if < 0 then it's root
// and stores negative rank
FindUnion(int _n = 0) : n(_n), parent(_n + 1, -1) {}
int find(int x) {
if (parent[x] < 0) {
return x;
}
return parent[x] = find(parent[x]);
}
void join(int x, int y) {
x = find(x);
y = find(y);
if (parent[x] < parent[y]) {
parent[y] = x;
} else if (parent[x] > parent[y]) {
parent[x] = y;
} else if (x != y) {
parent[y] = x;
--parent[x]; // Decrease negative rank
}
}
};
///// END FIND_UNION WITH PATH COMPRESSION AND RANKS
double get_time() {
auto now = chrono::system_clock::now();
auto duration = now.time_since_epoch();
double timestamp = chrono::duration<double>(duration).count();
return timestamp;
}
struct Elem {
int count;
int party;
bool operator<(const Elem &other) const { return count > other.count; }
};
struct Case {
int n, m, k;
vi parties;
// vector<vi> adj;
FindUnion fu;
vector<int> count_by_party;
priority_queue<Elem> q;
vector<bool> visited;
vector<vi> party_to_index;
vector<unordered_set<int>> compressed_adj;
void debug_vars() {
DEB("n=" << n << " m=" << m << " " << k << "\n");
DEB("\n");
DEB("find-union:\n");
FOR(i, 1, n) { DEB(" i=" << i << ": " << fu.find(i) << "\n"); }
DEB("\n");
DEB("compressed-adj:\n");
FOR(i, 1, n) {
DEB(" i=" << i << ": " << compressed_adj[fu.find(i)] << "\n");
}
DEB("\n");
DEB("count_by_party:\n");
FOR(p, 1, k) { DEB(" p=" << p << ": " << count_by_party[p] << "\n"); }
DEB("\n");
DEB("party_to_index:\n");
FOR(p, 1, k) { DEB(" p=" << p << ": " << party_to_index[p] << "\n"); }
DEB("\n");
}
void solve() {
read_graph();
fill_initial_fu();
count_initial_colors_by_party();
calc_party_to_index();
#ifdef DEBUG
debug_vars();
#endif
DEB("solve before process_on_pq\n");
bool result = process_on_pq();
DEB("solve after process_on_pq\n");
cout << (result ? "TAK" : "NIE") << "\n";
}
void read_graph() {
cin >> n >> m >> k;
parties.resize(n + 1);
FOR(i, 1, n) { cin >> parties[i]; }
// adj.resize(n + 1);
compressed_adj.resize(n + 1);
REP(j, m) {
int a, b;
cin >> a >> b;
// adj[a].PB(b);
// adj[b].PB(a);
compressed_adj[a].emplace(b);
compressed_adj[b].emplace(a);
}
}
void join_and_compress(int a, int b) {
// DEB("join and compress: " << a << " & " << b << "\n");
int parent_a = fu.find(a);
int parent_b = fu.find(b);
fu.join(a, b);
int new_parent = fu.find(a);
int old_parent = (parent_a != new_parent) ? parent_a : parent_b;
if (old_parent != new_parent) {
int smaller, larger;
if (SZ(compressed_adj[new_parent]) < SZ(compressed_adj[old_parent])) {
// swap(compressed_adj[new_parent], compressed_adj[old_parent]);
smaller = new_parent;
larger = old_parent;
} else {
smaller = old_parent;
larger = new_parent;
}
for (const auto &x : compressed_adj[smaller]) {
int parent_x = fu.find(x);
if (parent_x != larger) {
compressed_adj[larger].emplace(parent_x);
}
}
compressed_adj[smaller].clear();
if (smaller == new_parent) {
swap(compressed_adj[smaller], compressed_adj[larger]);
}
}
}
void fill_initial_fu() {
fu = FindUnion(n + 1);
FOR(a, 1, n) {
int parent_a = fu.find(a);
auto adj_copy = compressed_adj[parent_a];
for (auto b : adj_copy) {
int parent_b = fu.find(b);
if (parties[parent_a] == parties[parent_b]) {
join_and_compress(parent_a, parent_b);
}
}
}
}
void count_initial_colors_by_party() {
vector<unordered_map<int, int>> color_counts_by_party(k + 1);
FOR(i, 1, n) {
int party = parties[i];
int color = fu.find(i);
++color_counts_by_party[party][color];
}
count_by_party.resize(k + 1);
FOR(party, 1, k) {
count_by_party[party] = SZ(color_counts_by_party[party]);
}
}
void calc_party_to_index() {
party_to_index.resize(k + 1);
FOR(i, 1, n) {
int party = parties[i];
party_to_index[party].PB(i);
}
}
void prune_compressed_adj(int x, bool prune_visited) {
unordered_set<int> pruned;
for (auto y : compressed_adj[x]) {
int parent_y = fu.find(y);
if (prune_visited) {
if (not visited[parent_y]) {
pruned.emplace(parent_y);
}
} else {
pruned.emplace(parent_y);
}
}
swap(compressed_adj[x], pruned);
}
bool process_on_pq() {
double absorb_time = 0;
double neighbor_time = 0;
double last_iter_time = 0;
FOR(party, 1, k) {
q.push({.count = count_by_party[party], .party = party});
}
visited.resize(k + 1);
int cnt_pq = 0;
int cnt_pq_real = 0;
while (not q.empty()) {
// DEB("pq while begin\n");
const auto [count, party] = q.top();
// DEB("pq top: count=" << count << " party=" << party << "\n");
++cnt_pq;
q.pop();
if (visited[party]) {
continue;
}
DEB("REAL pq top: count=" << count << " party=" << party << "\n");
++cnt_pq_real;
if (count > 1) {
return false;
}
visited[party] = true;
// Absorb visited:
double absorb_start = get_time();
// DEB("before SZ(party_to_index[party]) = " << SZ(party_to_index[party])
// << "\n");
unordered_set<int> absorbed;
for (int x : party_to_index[party]) {
int parent_x = fu.find(x);
absorbed.emplace(parent_x);
// DEB(" SZ(adj) = " << SZ(compressed_adj[parent_x]) << "\n");
prune_compressed_adj(parent_x, false);
// DEB(" SZ(adj) pruned = " << SZ(compressed_adj[parent_x]) << "\n");
auto adj_copy = compressed_adj[parent_x];
for (int y : adj_copy) {
int parent_y = fu.find(y);
int party_y = parties[parent_y];
if (visited[party_y]) {
absorbed.emplace(parent_y);
if (parent_x != parent_y) {
join_and_compress(parent_x, parent_y);
}
}
}
}
absorb_time += get_time() - absorb_start;
// DEB("after SZ(absorbed) = " << SZ(absorbed) << "\n");
// DEB("before neighbors_by_party_color\n");
double neighbor_start = get_time();
unordered_map<int, unordered_map<int, int>> neighbors_by_party_color;
for (int x : absorbed) {
int parent_x = fu.find(x);
// DEB(" before prune adj = " << SZ(compressed_adj[parent_x]) << "\n");
// prune_compressed_adj(parent_x);
// DEB(" after prune adj = " << SZ(compressed_adj[parent_x]) << "\n");
for (int y : compressed_adj[parent_x]) {
int neighbor_color = fu.find(y);
int neighbor_party = parties[neighbor_color]; // czy lepiej y?
if (not visited[neighbor_party]) {
++neighbors_by_party_color[neighbor_party][neighbor_color];
}
}
}
neighbor_time += get_time() - neighbor_start;
// DEB("after neighbors_by_party_color SZ=" <<
// SZ(neighbors_by_party_color)
// << "\n");
// DEB("before last iter\n");
double last_iter_start = get_time();
for (const auto &[neighbor_party, neighbor_colors] :
neighbors_by_party_color) {
// DEB(" neighbor: party=" << neighbor_party
// << " colors=" << neighbor_colors << "\n");
int joins_count = SZ(neighbor_colors) - 1;
if (joins_count > 0) {
count_by_party[neighbor_party] -= joins_count;
if (not visited[neighbor_party]) {
q.push({.count = count_by_party[neighbor_party],
.party = neighbor_party});
}
}
vi colors;
for (const auto &[neighbor_color, cnt] : neighbor_colors) {
colors.PB(neighbor_color);
}
if (SZ(colors) > 0) {
int color = colors.front();
for (int other_color : colors) {
join_and_compress(color, other_color);
}
}
}
last_iter_time += get_time() - last_iter_start;
// for (int x : absorbed) {
// int parent_x = fu.find(x);
// prune_compressed_adj(parent_x, true);
// }
// DEB("after last iter\n");
// DEB("find-union after:\n");
// FOR(i, 1, n) { DEB(" i=" << i << ": " << fu.find(i) << "\n"); }
// DEB("\n");
// DEB("compressed-adj after:\n");
// FOR(i, 1, n) {
// DEB(" i=" << i << ": " << compressed_adj[fu.find(i)] << "\n");
// }
// DEB("\n");
}
DEB("cnt_pq = " << cnt_pq << "\n");
DEB("cnt_pq_real = " << cnt_pq_real << "\n");
cerr.precision(3);
DEB("absorb_time: " << absorb_time << "\n");
DEB("neighbor_time: " << neighbor_time << "\n");
DEB("last_iter_time: " << last_iter_time << "\n");
return true;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int z = 1;
cin >> z;
while (z--) {
Case().solve();
}
}
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | #include <algorithm> #include <bitset> #include <chrono> #include <cmath> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef unsigned int uint; const int INF = 1e9 + 9; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "vector["; for (const auto &e : v) { os << e << ", "; } os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &v) { os << "unordered_set["; for (const auto &e : v) { os << e << ", "; } os << "]"; return os; } template <typename K, typename V> ostream &operator<<(ostream &os, const unordered_map<K, V> &m) { os << "unordered_map["; for (const auto &[k, v] : m) { os << "(" << k << "," << v << "), "; } os << "]"; return os; } ///// BEGIN FIND_UNION WITH PATH COMPRESSION AND RANKS struct FindUnion { int n; vector<int> parent; // if >= 0 then it's parent node, if < 0 then it's root // and stores negative rank FindUnion(int _n = 0) : n(_n), parent(_n + 1, -1) {} int find(int x) { if (parent[x] < 0) { return x; } return parent[x] = find(parent[x]); } void join(int x, int y) { x = find(x); y = find(y); if (parent[x] < parent[y]) { parent[y] = x; } else if (parent[x] > parent[y]) { parent[x] = y; } else if (x != y) { parent[y] = x; --parent[x]; // Decrease negative rank } } }; ///// END FIND_UNION WITH PATH COMPRESSION AND RANKS double get_time() { auto now = chrono::system_clock::now(); auto duration = now.time_since_epoch(); double timestamp = chrono::duration<double>(duration).count(); return timestamp; } struct Elem { int count; int party; bool operator<(const Elem &other) const { return count > other.count; } }; struct Case { int n, m, k; vi parties; // vector<vi> adj; FindUnion fu; vector<int> count_by_party; priority_queue<Elem> q; vector<bool> visited; vector<vi> party_to_index; vector<unordered_set<int>> compressed_adj; void debug_vars() { DEB("n=" << n << " m=" << m << " " << k << "\n"); DEB("\n"); DEB("find-union:\n"); FOR(i, 1, n) { DEB(" i=" << i << ": " << fu.find(i) << "\n"); } DEB("\n"); DEB("compressed-adj:\n"); FOR(i, 1, n) { DEB(" i=" << i << ": " << compressed_adj[fu.find(i)] << "\n"); } DEB("\n"); DEB("count_by_party:\n"); FOR(p, 1, k) { DEB(" p=" << p << ": " << count_by_party[p] << "\n"); } DEB("\n"); DEB("party_to_index:\n"); FOR(p, 1, k) { DEB(" p=" << p << ": " << party_to_index[p] << "\n"); } DEB("\n"); } void solve() { read_graph(); fill_initial_fu(); count_initial_colors_by_party(); calc_party_to_index(); #ifdef DEBUG debug_vars(); #endif DEB("solve before process_on_pq\n"); bool result = process_on_pq(); DEB("solve after process_on_pq\n"); cout << (result ? "TAK" : "NIE") << "\n"; } void read_graph() { cin >> n >> m >> k; parties.resize(n + 1); FOR(i, 1, n) { cin >> parties[i]; } // adj.resize(n + 1); compressed_adj.resize(n + 1); REP(j, m) { int a, b; cin >> a >> b; // adj[a].PB(b); // adj[b].PB(a); compressed_adj[a].emplace(b); compressed_adj[b].emplace(a); } } void join_and_compress(int a, int b) { // DEB("join and compress: " << a << " & " << b << "\n"); int parent_a = fu.find(a); int parent_b = fu.find(b); fu.join(a, b); int new_parent = fu.find(a); int old_parent = (parent_a != new_parent) ? parent_a : parent_b; if (old_parent != new_parent) { int smaller, larger; if (SZ(compressed_adj[new_parent]) < SZ(compressed_adj[old_parent])) { // swap(compressed_adj[new_parent], compressed_adj[old_parent]); smaller = new_parent; larger = old_parent; } else { smaller = old_parent; larger = new_parent; } for (const auto &x : compressed_adj[smaller]) { int parent_x = fu.find(x); if (parent_x != larger) { compressed_adj[larger].emplace(parent_x); } } compressed_adj[smaller].clear(); if (smaller == new_parent) { swap(compressed_adj[smaller], compressed_adj[larger]); } } } void fill_initial_fu() { fu = FindUnion(n + 1); FOR(a, 1, n) { int parent_a = fu.find(a); auto adj_copy = compressed_adj[parent_a]; for (auto b : adj_copy) { int parent_b = fu.find(b); if (parties[parent_a] == parties[parent_b]) { join_and_compress(parent_a, parent_b); } } } } void count_initial_colors_by_party() { vector<unordered_map<int, int>> color_counts_by_party(k + 1); FOR(i, 1, n) { int party = parties[i]; int color = fu.find(i); ++color_counts_by_party[party][color]; } count_by_party.resize(k + 1); FOR(party, 1, k) { count_by_party[party] = SZ(color_counts_by_party[party]); } } void calc_party_to_index() { party_to_index.resize(k + 1); FOR(i, 1, n) { int party = parties[i]; party_to_index[party].PB(i); } } void prune_compressed_adj(int x, bool prune_visited) { unordered_set<int> pruned; for (auto y : compressed_adj[x]) { int parent_y = fu.find(y); if (prune_visited) { if (not visited[parent_y]) { pruned.emplace(parent_y); } } else { pruned.emplace(parent_y); } } swap(compressed_adj[x], pruned); } bool process_on_pq() { double absorb_time = 0; double neighbor_time = 0; double last_iter_time = 0; FOR(party, 1, k) { q.push({.count = count_by_party[party], .party = party}); } visited.resize(k + 1); int cnt_pq = 0; int cnt_pq_real = 0; while (not q.empty()) { // DEB("pq while begin\n"); const auto [count, party] = q.top(); // DEB("pq top: count=" << count << " party=" << party << "\n"); ++cnt_pq; q.pop(); if (visited[party]) { continue; } DEB("REAL pq top: count=" << count << " party=" << party << "\n"); ++cnt_pq_real; if (count > 1) { return false; } visited[party] = true; // Absorb visited: double absorb_start = get_time(); // DEB("before SZ(party_to_index[party]) = " << SZ(party_to_index[party]) // << "\n"); unordered_set<int> absorbed; for (int x : party_to_index[party]) { int parent_x = fu.find(x); absorbed.emplace(parent_x); // DEB(" SZ(adj) = " << SZ(compressed_adj[parent_x]) << "\n"); prune_compressed_adj(parent_x, false); // DEB(" SZ(adj) pruned = " << SZ(compressed_adj[parent_x]) << "\n"); auto adj_copy = compressed_adj[parent_x]; for (int y : adj_copy) { int parent_y = fu.find(y); int party_y = parties[parent_y]; if (visited[party_y]) { absorbed.emplace(parent_y); if (parent_x != parent_y) { join_and_compress(parent_x, parent_y); } } } } absorb_time += get_time() - absorb_start; // DEB("after SZ(absorbed) = " << SZ(absorbed) << "\n"); // DEB("before neighbors_by_party_color\n"); double neighbor_start = get_time(); unordered_map<int, unordered_map<int, int>> neighbors_by_party_color; for (int x : absorbed) { int parent_x = fu.find(x); // DEB(" before prune adj = " << SZ(compressed_adj[parent_x]) << "\n"); // prune_compressed_adj(parent_x); // DEB(" after prune adj = " << SZ(compressed_adj[parent_x]) << "\n"); for (int y : compressed_adj[parent_x]) { int neighbor_color = fu.find(y); int neighbor_party = parties[neighbor_color]; // czy lepiej y? if (not visited[neighbor_party]) { ++neighbors_by_party_color[neighbor_party][neighbor_color]; } } } neighbor_time += get_time() - neighbor_start; // DEB("after neighbors_by_party_color SZ=" << // SZ(neighbors_by_party_color) // << "\n"); // DEB("before last iter\n"); double last_iter_start = get_time(); for (const auto &[neighbor_party, neighbor_colors] : neighbors_by_party_color) { // DEB(" neighbor: party=" << neighbor_party // << " colors=" << neighbor_colors << "\n"); int joins_count = SZ(neighbor_colors) - 1; if (joins_count > 0) { count_by_party[neighbor_party] -= joins_count; if (not visited[neighbor_party]) { q.push({.count = count_by_party[neighbor_party], .party = neighbor_party}); } } vi colors; for (const auto &[neighbor_color, cnt] : neighbor_colors) { colors.PB(neighbor_color); } if (SZ(colors) > 0) { int color = colors.front(); for (int other_color : colors) { join_and_compress(color, other_color); } } } last_iter_time += get_time() - last_iter_start; // for (int x : absorbed) { // int parent_x = fu.find(x); // prune_compressed_adj(parent_x, true); // } // DEB("after last iter\n"); // DEB("find-union after:\n"); // FOR(i, 1, n) { DEB(" i=" << i << ": " << fu.find(i) << "\n"); } // DEB("\n"); // DEB("compressed-adj after:\n"); // FOR(i, 1, n) { // DEB(" i=" << i << ": " << compressed_adj[fu.find(i)] << "\n"); // } // DEB("\n"); } DEB("cnt_pq = " << cnt_pq << "\n"); DEB("cnt_pq_real = " << cnt_pq_real << "\n"); cerr.precision(3); DEB("absorb_time: " << absorb_time << "\n"); DEB("neighbor_time: " << neighbor_time << "\n"); DEB("last_iter_time: " << last_iter_time << "\n"); return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int z = 1; cin >> z; while (z--) { Case().solve(); } } |
English