#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define vii vector<pair<int,int>>
#define vi vector<int>
#define pll pair<ll, ll>
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define count_bits(x) __builtin_popcountll((x))
const ll M = 1e9+7;
const ll INF = 1e9;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int MAXN = 1e5 + 5;
struct chash{
int operator()(int x) const {return x ^ 10458213;}
};
int repbialy[MAXN];
map<int,int> amb[MAXN];
int repkolor[MAXN];
int cntkolor[MAXN];
int kolor[MAXN];
int cnt[MAXN];
vii wych[MAXN];
int isdone[MAXN];
int Find1(int a){
if(repkolor[a] == a) return a;
repkolor[a] = Find1(repkolor[a]);
return repkolor[a];
}
int Find2(int a){
if(repbialy[a] == a) return a;
return Find2(repbialy[a]);
}
void solve(){
int n, m, k;
cin >> n >> m >> k;
int a, b;
for(int i = 1; i <= n; ++i){
cin >> a;
kolor[i] = a;
cnt[a]++;
repbialy[i] = i;
amb[i][a] = i;
repkolor[i] = i;
cntkolor[i] = 1;
}
for(int i = 1; i <= m; ++i){
cin >> a >> b;
if(a == b) continue;
if(kolor[a] == kolor[b]){
int x = Find1(a);
int y = Find1(b);
if(x == y) continue;
repkolor[x] = y;
cntkolor[y] += cntkolor[x];
}
wych[kolor[a]].push_back({a, b});
wych[kolor[b]].push_back({b, a});
}
queue<int> q;
for(int i = 1; i <= n; ++i){
if(repkolor[i] == i and cntkolor[i] == cnt[kolor[i]]){
q.push(kolor[i]);
}
}
int lft = 0;
for(int i = 1; i <= k; ++i){
if(cnt[i] > 0) lft++;
}
while(!q.empty()){
auto u = q.front();
q.pop();
// cout << u << " u\n";
if(isdone[u] == 1) continue;
isdone[u] = 1;
lft--;
vii acc;
vii plus;
for(auto v : wych[u]){
if(isdone[kolor[v.first]] == 1 and isdone[kolor[v.second]] == 1){
acc.push_back(v);
}
else{
plus.push_back(v);
}
}
for(auto v : acc){
// cout << v.first << " " << v.second << " UNION REAL\n";
int x = Find2(v.first);
int y = Find2(v.second);
if(x == y) continue;
if(amb[x].size() > amb[y].size()){
swap(x, y);
}
for(auto t : amb[x]){
// cout << t.first << " " << t.second << " kolor, ambasador\n";
// cout << amb[y][t.first] << " ODPOWIEDZ\n";
if(amb[y][t.first] == 0){
amb[y][t.first] = t.second;
}
else{
int vl = amb[y][t.first];
int x = Find1(vl);
int y = Find1(t.second);
if(x == y) continue;
repkolor[x] = y;
cntkolor[y] += cntkolor[x];
if(cntkolor[y] == cnt[kolor[y]]){
q.push(kolor[y]);
}
amb[y][t.first] = y;
}
}
repbialy[x] = y;
}
for(auto t : plus){
// cout << t.first << " " << t.second << " plus\n";
int kt = Find2(t.first);
int kl = kolor[t.second];
// cout << amb[kt][kl] << " ans\n";
if(amb[kt][kl] == 0){
amb[kt][kl] = t.second;
}
else{
int x = Find1(amb[kt][kl]);
int y = Find1(t.second);
if(x == y) continue;
repkolor[x] = y;
cntkolor[y] += cntkolor[x];
// cout << y << " nowy\n";
// cout << cntkolor[y] << " " << cnt[kolor[y]] << " W\n";
if(cntkolor[y] == cnt[kolor[y]]){
q.push(kolor[y]);
}
amb[kt][kl] = y;
}
}
}
if(lft == 0){
cout << "TAK\n";
}
else{
cout << "NIE\n";
}
for(int i = 1; i <= n; ++i){
kolor[i] = 0;
cntkolor[i] = 0;
repkolor[i] = 0;
amb[i].clear();
repbialy[i] = 0;
}
for(int i = 1; i <= k; ++i){
wych[i].clear();
cnt[i] = 0;
isdone[i] = 0;
}
return;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
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 187 188 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; #define pii pair<int,int> #define vii vector<pair<int,int>> #define vi vector<int> #define pll pair<ll, ll> #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define count_bits(x) __builtin_popcountll((x)) const ll M = 1e9+7; const ll INF = 1e9; //mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll los(ll a, ll b) {return a + (mt() % (b-a+1));} typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; const int MAXN = 1e5 + 5; struct chash{ int operator()(int x) const {return x ^ 10458213;} }; int repbialy[MAXN]; map<int,int> amb[MAXN]; int repkolor[MAXN]; int cntkolor[MAXN]; int kolor[MAXN]; int cnt[MAXN]; vii wych[MAXN]; int isdone[MAXN]; int Find1(int a){ if(repkolor[a] == a) return a; repkolor[a] = Find1(repkolor[a]); return repkolor[a]; } int Find2(int a){ if(repbialy[a] == a) return a; return Find2(repbialy[a]); } void solve(){ int n, m, k; cin >> n >> m >> k; int a, b; for(int i = 1; i <= n; ++i){ cin >> a; kolor[i] = a; cnt[a]++; repbialy[i] = i; amb[i][a] = i; repkolor[i] = i; cntkolor[i] = 1; } for(int i = 1; i <= m; ++i){ cin >> a >> b; if(a == b) continue; if(kolor[a] == kolor[b]){ int x = Find1(a); int y = Find1(b); if(x == y) continue; repkolor[x] = y; cntkolor[y] += cntkolor[x]; } wych[kolor[a]].push_back({a, b}); wych[kolor[b]].push_back({b, a}); } queue<int> q; for(int i = 1; i <= n; ++i){ if(repkolor[i] == i and cntkolor[i] == cnt[kolor[i]]){ q.push(kolor[i]); } } int lft = 0; for(int i = 1; i <= k; ++i){ if(cnt[i] > 0) lft++; } while(!q.empty()){ auto u = q.front(); q.pop(); // cout << u << " u\n"; if(isdone[u] == 1) continue; isdone[u] = 1; lft--; vii acc; vii plus; for(auto v : wych[u]){ if(isdone[kolor[v.first]] == 1 and isdone[kolor[v.second]] == 1){ acc.push_back(v); } else{ plus.push_back(v); } } for(auto v : acc){ // cout << v.first << " " << v.second << " UNION REAL\n"; int x = Find2(v.first); int y = Find2(v.second); if(x == y) continue; if(amb[x].size() > amb[y].size()){ swap(x, y); } for(auto t : amb[x]){ // cout << t.first << " " << t.second << " kolor, ambasador\n"; // cout << amb[y][t.first] << " ODPOWIEDZ\n"; if(amb[y][t.first] == 0){ amb[y][t.first] = t.second; } else{ int vl = amb[y][t.first]; int x = Find1(vl); int y = Find1(t.second); if(x == y) continue; repkolor[x] = y; cntkolor[y] += cntkolor[x]; if(cntkolor[y] == cnt[kolor[y]]){ q.push(kolor[y]); } amb[y][t.first] = y; } } repbialy[x] = y; } for(auto t : plus){ // cout << t.first << " " << t.second << " plus\n"; int kt = Find2(t.first); int kl = kolor[t.second]; // cout << amb[kt][kl] << " ans\n"; if(amb[kt][kl] == 0){ amb[kt][kl] = t.second; } else{ int x = Find1(amb[kt][kl]); int y = Find1(t.second); if(x == y) continue; repkolor[x] = y; cntkolor[y] += cntkolor[x]; // cout << y << " nowy\n"; // cout << cntkolor[y] << " " << cnt[kolor[y]] << " W\n"; if(cntkolor[y] == cnt[kolor[y]]){ q.push(kolor[y]); } amb[kt][kl] = y; } } } if(lft == 0){ cout << "TAK\n"; } else{ cout << "NIE\n"; } for(int i = 1; i <= n; ++i){ kolor[i] = 0; cntkolor[i] = 0; repkolor[i] = 0; amb[i].clear(); repbialy[i] = 0; } for(int i = 1; i <= k; ++i){ wych[i].clear(); cnt[i] = 0; isdone[i] = 0; } return; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while(t--){ solve(); } return 0; } |
English