#include <bits/stdc++.h>
using pii = std::pair<int, int>;
struct fu_t {
int idx;
std::set<pii> edges; //party, ptr/idx
std::set<int> edges_to_deleted;
std::set<int> parties_with_2;
bool deleted = false;
};
void solve() {
int n, m, k;
std::cin >> n >> m >> k;
std::vector<int> party(n),
partyClusters(k + 1, 0),
partySize(k + 1);
std::vector<fu_t> fu(n);
std::vector<int> toBeDeleted;
auto find = [&](this auto find, int i) {
if (i == fu[i].idx) {
return i;
}
fu[i].idx = find(fu[i].idx);
return fu[i].idx;
};
auto join = [&](this auto join, int a, int b, std::vector<int> &toBeDeleted) {
a = find(a);
b = find(b);
if (a == b) {
return;
}
if (fu[b].edges.size() > fu[a].edges.size()) {
std::swap(a, b);
}
fu[b].idx = a;
fu[a].deleted |= fu[b].deleted;
if (party[a] == party[b]) {
partyClusters[party[a]]--;
if (partyClusters[party[a]] == 1) {
toBeDeleted.push_back(a);
}
}
for (auto e : fu[b].edges) {
if (find(e.second) == find(a)) {
continue;
}
auto it = fu[a].edges.lower_bound({e.first, -1});
if (it != fu[a].edges.end() && it->first == e.first) {
fu[a].parties_with_2.insert(e.first);
}
fu[a].edges.insert(e);
}
for (auto e : fu[b].edges_to_deleted) {
fu[a].edges_to_deleted.insert(e);
}
};
auto del = [&](this auto del, int i, std::vector<int> &toBeDeleted) {
i = find(i);
fu[i].deleted = true;
for (auto e : fu[i].edges) {
int b = find(e.second);
if (b == i || fu[b].deleted) {
continue;
}
fu[b].edges_to_deleted.insert(i);
}
for (auto e : fu[i].edges_to_deleted) {
join(i, e, toBeDeleted);
}
i = find(i);
fu[i].edges_to_deleted.clear();
for (auto e : fu[i].parties_with_2) {
auto it1 = fu[i].edges.lower_bound({e, -1});
auto it2 = fu[i].edges.lower_bound({e, 1e6});
if (it1 == it2) continue;
while (std::next(it1, 1) != it2) {
join(it1->second, std::next(it1, 1)->second, toBeDeleted);
fu[i].edges.erase(std::next(it1, 1));
}
}
fu[i].parties_with_2.clear();
};
for (int i = 0; i < n; i++) {
std::cin >> party[i];
fu[i].idx = i;
partyClusters[party[i]]++;
partySize[party[i]]++;
}
for (int i = 0, a, b; i < m; i++) {
std::cin >> a >> b;
a--; b--;
if (party[a] == party[b]) {
join(a, b, toBeDeleted);
} else {
a = find(a);
b = find(b);
auto ita = fu[a].edges.lower_bound({party[b], -1});
auto itb = fu[b].edges.lower_bound({party[a], -1});
if (ita != fu[a].edges.end() && ita->first == party[b]) {
fu[a].parties_with_2.insert(party[b]);
}
if (itb != fu[b].edges.end() && itb->first == party[a]) {
fu[b].parties_with_2.insert(party[a]);
}
fu[a].edges.insert({party[b], b});
fu[b].edges.insert({party[a], a});
}
}
for (int i = 0; i < n; i++) {
if (partySize[party[i]] == 1) {
toBeDeleted.push_back(i);
}
}
while (!toBeDeleted.empty()) {
int x = toBeDeleted.back();
toBeDeleted.pop_back();
del(x, toBeDeleted);
}
int ok = true;
for (int i = 0; i < k + 1; i++) {
if (partyClusters[i] > 1) {
ok = false;
}
}
std::cout << (ok ? "TAK" : "NIE") << "\n";
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while(T--) {
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 | #include <bits/stdc++.h> using pii = std::pair<int, int>; struct fu_t { int idx; std::set<pii> edges; //party, ptr/idx std::set<int> edges_to_deleted; std::set<int> parties_with_2; bool deleted = false; }; void solve() { int n, m, k; std::cin >> n >> m >> k; std::vector<int> party(n), partyClusters(k + 1, 0), partySize(k + 1); std::vector<fu_t> fu(n); std::vector<int> toBeDeleted; auto find = [&](this auto find, int i) { if (i == fu[i].idx) { return i; } fu[i].idx = find(fu[i].idx); return fu[i].idx; }; auto join = [&](this auto join, int a, int b, std::vector<int> &toBeDeleted) { a = find(a); b = find(b); if (a == b) { return; } if (fu[b].edges.size() > fu[a].edges.size()) { std::swap(a, b); } fu[b].idx = a; fu[a].deleted |= fu[b].deleted; if (party[a] == party[b]) { partyClusters[party[a]]--; if (partyClusters[party[a]] == 1) { toBeDeleted.push_back(a); } } for (auto e : fu[b].edges) { if (find(e.second) == find(a)) { continue; } auto it = fu[a].edges.lower_bound({e.first, -1}); if (it != fu[a].edges.end() && it->first == e.first) { fu[a].parties_with_2.insert(e.first); } fu[a].edges.insert(e); } for (auto e : fu[b].edges_to_deleted) { fu[a].edges_to_deleted.insert(e); } }; auto del = [&](this auto del, int i, std::vector<int> &toBeDeleted) { i = find(i); fu[i].deleted = true; for (auto e : fu[i].edges) { int b = find(e.second); if (b == i || fu[b].deleted) { continue; } fu[b].edges_to_deleted.insert(i); } for (auto e : fu[i].edges_to_deleted) { join(i, e, toBeDeleted); } i = find(i); fu[i].edges_to_deleted.clear(); for (auto e : fu[i].parties_with_2) { auto it1 = fu[i].edges.lower_bound({e, -1}); auto it2 = fu[i].edges.lower_bound({e, 1e6}); if (it1 == it2) continue; while (std::next(it1, 1) != it2) { join(it1->second, std::next(it1, 1)->second, toBeDeleted); fu[i].edges.erase(std::next(it1, 1)); } } fu[i].parties_with_2.clear(); }; for (int i = 0; i < n; i++) { std::cin >> party[i]; fu[i].idx = i; partyClusters[party[i]]++; partySize[party[i]]++; } for (int i = 0, a, b; i < m; i++) { std::cin >> a >> b; a--; b--; if (party[a] == party[b]) { join(a, b, toBeDeleted); } else { a = find(a); b = find(b); auto ita = fu[a].edges.lower_bound({party[b], -1}); auto itb = fu[b].edges.lower_bound({party[a], -1}); if (ita != fu[a].edges.end() && ita->first == party[b]) { fu[a].parties_with_2.insert(party[b]); } if (itb != fu[b].edges.end() && itb->first == party[a]) { fu[b].parties_with_2.insert(party[a]); } fu[a].edges.insert({party[b], b}); fu[b].edges.insert({party[a], a}); } } for (int i = 0; i < n; i++) { if (partySize[party[i]] == 1) { toBeDeleted.push_back(i); } } while (!toBeDeleted.empty()) { int x = toBeDeleted.back(); toBeDeleted.pop_back(); del(x, toBeDeleted); } int ok = true; for (int i = 0; i < k + 1; i++) { if (partyClusters[i] > 1) { ok = false; } } std::cout << (ok ? "TAK" : "NIE") << "\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int T; std::cin >> T; while(T--) { solve(); } } |
English