#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pb push_back
#define F first
#define S second
#define INF (ll)2e18
#define pii pair<int,int>
#define int ll
int n,m,k;
const int N = (int)1e5+5;
int cnt[N];
int cur_cnt[N];
unordered_map<int,int> color_roots[N];
int a[N];
vector<int> g[N];
vector<int> g2[N];
vector<int> nodes_color[N];
bool visited[N];
queue<int> q;
struct DSU{
vector<int> parent, size;
DSU() {}
DSU(int n){
parent.resize(n);
size.resize(n);
for (int i = 0; i < n; i++){
parent[i] = i;
size[i] = 1;
}
}
void reset(int n){
parent.resize(n);
size.resize(n);
for (int i = 0; i < n; i++){
parent[i] = i;
size[i] = 1;
}
}
int find(int u){
if (parent[u] == u) return u;
return parent[u] = find(parent[u]);
}
bool merge(int u, int v){
u = find(u);
v = find(v);
if (u == v) return false;
if (size[u] < size[v]) swap(u,v);
parent[v] = u;
size[u] += size[v];
return true;
}
};
DSU dsu;
void merge(int u, int v){
u = dsu.find(u), v = dsu.find(v);
if (u == v) return;
if (visited[a[v]]){ // full merge
if (color_roots[u].size() < color_roots[v].size()) swap(u,v);
for (auto [c,root] : color_roots[v]){
if (!color_roots[u].count(c)){
color_roots[u][c] = root;
} else {
if (dsu.merge(color_roots[u][c], color_roots[v][c])){
cur_cnt[c]++;
}
if (cur_cnt[c] == cnt[c]-1 && !visited[c]){
visited[c] = true;
q.push(c);
}
}
}
color_roots[v].clear();
dsu.parent[v] = u;
dsu.size[u] += dsu.size[v];
} else { // can't full merge
if (color_roots[u].count(a[v])){
if (dsu.merge(color_roots[u][a[v]], v)){
cur_cnt[a[v]]++;
}
if (cur_cnt[a[v]] == cnt[a[v]]-1 && !visited[a[v]]){
visited[a[v]] = true;
q.push(a[v]);
}
} else {
color_roots[u][a[v]] = v;
}
}
}
void solve() {
cin >> n >> m >> k;
dsu.reset(n+5);
for (int i = 0; i < n; i++){
cin >> a[i];
}
for (int i = 0; i < m; i++){
int u,v; cin >> u >> v;
u--,v--;
if (u == v) continue;
g2[u].pb(v);
g2[v].pb(u);
if (a[u] == a[v]){
dsu.merge(u,v);
}
}
for (int i = 0; i < n; i++){
if (dsu.find(i) == i){
nodes_color[a[i]].pb(i);
cnt[a[i]]++;
}
for (int v : g2[i]){
if (dsu.find(i) != dsu.find(v)){
g[dsu.find(i)].pb(dsu.find(v));
}
}
}
for (int i = 0; i < n; i++){
if (dsu.find(i) == i && cnt[a[i]] == 1){
visited[a[i]] = true;
q.push(a[i]);
}
}
while(!q.empty()){
int c = q.front();
q.pop();
for (int u : nodes_color[c]){
for (int v : g[u]){
merge(u,v);
}
}
}
bool ok = true;
for (int i = 0; i < n; i++){
if (cur_cnt[a[i]] != cnt[a[i]]-1){
ok = false;
break;
}
}
cout << (ok ? "TAK" : "NIE") << "\n";
for (int i = 0; i < n; i++){
color_roots[i].clear();
g[i].clear();
g2[i].clear();
nodes_color[a[i]].clear();
cnt[a[i]] = 0;
cur_cnt[a[i]] = 0;
visited[a[i]] = false;
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
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 181 182 183 184 185 186 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define ll long long #define ld long double #define pll pair<ll,ll> #define pb push_back #define F first #define S second #define INF (ll)2e18 #define pii pair<int,int> #define int ll int n,m,k; const int N = (int)1e5+5; int cnt[N]; int cur_cnt[N]; unordered_map<int,int> color_roots[N]; int a[N]; vector<int> g[N]; vector<int> g2[N]; vector<int> nodes_color[N]; bool visited[N]; queue<int> q; struct DSU{ vector<int> parent, size; DSU() {} DSU(int n){ parent.resize(n); size.resize(n); for (int i = 0; i < n; i++){ parent[i] = i; size[i] = 1; } } void reset(int n){ parent.resize(n); size.resize(n); for (int i = 0; i < n; i++){ parent[i] = i; size[i] = 1; } } int find(int u){ if (parent[u] == u) return u; return parent[u] = find(parent[u]); } bool merge(int u, int v){ u = find(u); v = find(v); if (u == v) return false; if (size[u] < size[v]) swap(u,v); parent[v] = u; size[u] += size[v]; return true; } }; DSU dsu; void merge(int u, int v){ u = dsu.find(u), v = dsu.find(v); if (u == v) return; if (visited[a[v]]){ // full merge if (color_roots[u].size() < color_roots[v].size()) swap(u,v); for (auto [c,root] : color_roots[v]){ if (!color_roots[u].count(c)){ color_roots[u][c] = root; } else { if (dsu.merge(color_roots[u][c], color_roots[v][c])){ cur_cnt[c]++; } if (cur_cnt[c] == cnt[c]-1 && !visited[c]){ visited[c] = true; q.push(c); } } } color_roots[v].clear(); dsu.parent[v] = u; dsu.size[u] += dsu.size[v]; } else { // can't full merge if (color_roots[u].count(a[v])){ if (dsu.merge(color_roots[u][a[v]], v)){ cur_cnt[a[v]]++; } if (cur_cnt[a[v]] == cnt[a[v]]-1 && !visited[a[v]]){ visited[a[v]] = true; q.push(a[v]); } } else { color_roots[u][a[v]] = v; } } } void solve() { cin >> n >> m >> k; dsu.reset(n+5); for (int i = 0; i < n; i++){ cin >> a[i]; } for (int i = 0; i < m; i++){ int u,v; cin >> u >> v; u--,v--; if (u == v) continue; g2[u].pb(v); g2[v].pb(u); if (a[u] == a[v]){ dsu.merge(u,v); } } for (int i = 0; i < n; i++){ if (dsu.find(i) == i){ nodes_color[a[i]].pb(i); cnt[a[i]]++; } for (int v : g2[i]){ if (dsu.find(i) != dsu.find(v)){ g[dsu.find(i)].pb(dsu.find(v)); } } } for (int i = 0; i < n; i++){ if (dsu.find(i) == i && cnt[a[i]] == 1){ visited[a[i]] = true; q.push(a[i]); } } while(!q.empty()){ int c = q.front(); q.pop(); for (int u : nodes_color[c]){ for (int v : g[u]){ merge(u,v); } } } bool ok = true; for (int i = 0; i < n; i++){ if (cur_cnt[a[i]] != cnt[a[i]]-1){ ok = false; break; } } cout << (ok ? "TAK" : "NIE") << "\n"; for (int i = 0; i < n; i++){ color_roots[i].clear(); g[i].clear(); g2[i].clear(); nodes_color[a[i]].clear(); cnt[a[i]] = 0; cur_cnt[a[i]] = 0; visited[a[i]] = false; } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; } |
English