#include <bits/stdc++.h>
using namespace std;
// for node_id, for campaign_id: nei_id
vector<map<int, vector<int>>> graph;
map<int, vector<int>> campaign_to_nodes;
vector<int> node_to_campaign;
class DSU {
public:
map<int, int> parent;
queue<int> que;
map<int, map<int, unsigned int>> parent_campaign_to_count;
int camp_left;
DSU(int n) {
camp_left = campaign_to_nodes.size();
for (int node_id = 1; node_id <= n; node_id++) {
parent[node_id] = node_id;
int c = node_to_campaign[node_id];
parent_campaign_to_count[node_id][c]++;
if (parent_campaign_to_count[node_id][c] == campaign_to_nodes[c].size()) {
que.push(c);
parent_campaign_to_count[node_id].erase(c);
}
}
for (int node_id = 1; node_id <= n; node_id++) {
int c = node_to_campaign[node_id];
int root_id = find(node_id);
for (int nei_id : graph[node_id][c]) {
root_id = unite(root_id, nei_id);
}
}
}
void read_from_que() {
while (!que.empty()) {
int camp = que.front();
que.pop();
for (int node_id : campaign_to_nodes[camp]) {
for (auto [c, neighbors] : graph[node_id]) {
for (int nei_id : neighbors) {
unite(node_id, nei_id);
}
}
}
camp_left--;
}
}
int find(int node_id) {
if (parent[node_id] == node_id) {
return node_id;
}
return parent[node_id] = find(parent[node_id]);
}
int unite(int id1, int id2) {
id1 = find(id1);
id2 = find(id2);
if (id1 == id2) {
return id1;
}
if (parent_campaign_to_count[id1].size() < parent_campaign_to_count[id2].size()) {
swap(id1, id2);
}
for (auto &[camp, count] : parent_campaign_to_count[id2]) {
parent_campaign_to_count[id1][camp] += count;
if (parent_campaign_to_count[id1][camp] == campaign_to_nodes[camp].size()) {
que.push(camp);
parent_campaign_to_count[id1].erase(camp);
}
}
parent_campaign_to_count[id2].clear();
return parent[id2] = id1;
}
};
DSU* dsu;
void test_case() {
int n, m, k;
cin >> n >> m >> k;
node_to_campaign.assign(n + 1, 0);
graph.assign(n + 1, map<int, vector<int>>());
for (int i = 1; i <= n; i++) {
cin >> node_to_campaign[i];
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
graph[u][node_to_campaign[v]].push_back(v);
graph[v][node_to_campaign[u]].push_back(u);
}
for (int i = 1; i <= n; i++) {
campaign_to_nodes[node_to_campaign[i]].push_back(i);
}
dsu = new DSU(n);
dsu->read_from_que();
cout << (!dsu->camp_left ? "TAK" : "NIE") << endl;
graph.clear();
campaign_to_nodes.clear();
node_to_campaign.clear();
delete dsu;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
test_case();
}
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 | #include <bits/stdc++.h> using namespace std; // for node_id, for campaign_id: nei_id vector<map<int, vector<int>>> graph; map<int, vector<int>> campaign_to_nodes; vector<int> node_to_campaign; class DSU { public: map<int, int> parent; queue<int> que; map<int, map<int, unsigned int>> parent_campaign_to_count; int camp_left; DSU(int n) { camp_left = campaign_to_nodes.size(); for (int node_id = 1; node_id <= n; node_id++) { parent[node_id] = node_id; int c = node_to_campaign[node_id]; parent_campaign_to_count[node_id][c]++; if (parent_campaign_to_count[node_id][c] == campaign_to_nodes[c].size()) { que.push(c); parent_campaign_to_count[node_id].erase(c); } } for (int node_id = 1; node_id <= n; node_id++) { int c = node_to_campaign[node_id]; int root_id = find(node_id); for (int nei_id : graph[node_id][c]) { root_id = unite(root_id, nei_id); } } } void read_from_que() { while (!que.empty()) { int camp = que.front(); que.pop(); for (int node_id : campaign_to_nodes[camp]) { for (auto [c, neighbors] : graph[node_id]) { for (int nei_id : neighbors) { unite(node_id, nei_id); } } } camp_left--; } } int find(int node_id) { if (parent[node_id] == node_id) { return node_id; } return parent[node_id] = find(parent[node_id]); } int unite(int id1, int id2) { id1 = find(id1); id2 = find(id2); if (id1 == id2) { return id1; } if (parent_campaign_to_count[id1].size() < parent_campaign_to_count[id2].size()) { swap(id1, id2); } for (auto &[camp, count] : parent_campaign_to_count[id2]) { parent_campaign_to_count[id1][camp] += count; if (parent_campaign_to_count[id1][camp] == campaign_to_nodes[camp].size()) { que.push(camp); parent_campaign_to_count[id1].erase(camp); } } parent_campaign_to_count[id2].clear(); return parent[id2] = id1; } }; DSU* dsu; void test_case() { int n, m, k; cin >> n >> m >> k; node_to_campaign.assign(n + 1, 0); graph.assign(n + 1, map<int, vector<int>>()); for (int i = 1; i <= n; i++) { cin >> node_to_campaign[i]; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; graph[u][node_to_campaign[v]].push_back(v); graph[v][node_to_campaign[u]].push_back(u); } for (int i = 1; i <= n; i++) { campaign_to_nodes[node_to_campaign[i]].push_back(i); } dsu = new DSU(n); dsu->read_from_que(); cout << (!dsu->camp_left ? "TAK" : "NIE") << endl; graph.clear(); campaign_to_nodes.clear(); node_to_campaign.clear(); delete dsu; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { test_case(); } return 0; } |
English