#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define BOOST ios_base::sync_with_stdio(0), cin.tie(0)
template<typename T>
using vc = vector<T>;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
vc<int> t;
vc<vc<int>> g;
vc<vc<int>> in;
vc<unordered_map<int, int>> mp;
vc<int> mark;
vc<int> cnt;
vc<int> uf;
vc<int> s;
queue<int> fulq;
queue<ii> uq;
int F(int x){
if(uf[x] == x) return x;
return uf[x] = F(uf[x]);
}
void U(int a, int b){
a = F(a), b = F(b);
if(a == b) return;
if(s[a] < s[b]) swap(a, b);
// printf("merge %d, %d\n", a, b);
uf[b] = a;
s[a] += s[b];
for(auto x : in[b]) in[a].pb(x);
if(mark[a] && mark[b]){
for(auto [party, xb] : mp[b]){
auto it = mp[a].find(party);
if(it != mp[a].end()){
auto xa = (*it).nd;
uq.push({xa, xb});
}
else mp[a][party] = xb;
}
}
if((int)in[a].size() == cnt[t[a]]) fulq.push(a);
}
// checked
void full(int x){
x = F(x);
// printf("full %d, kolor %d\n", x, t[x]);
mark[x] = 1;
for(auto it : in[x]){
for(auto jt : g[it]){
int y = F(jt);
// printf("full %d, node %d, sasiad %d, node %d\n", x, it, y, jt);
if(mark[y]){
uq.push({x, y});
continue;
}
// printf("alive x = %d, y = %d, t[%d] = %d\n", x, y, y, t[y]);
auto xd = mp[x].find(t[y]);
if(xd == mp[x].end()) mp[x][t[y]] = y;
else uq.push({(*xd).nd, y});
}
}
}
void solve(){
int n, m, k;
cin >> n >> m >> k;
t = mark = uf = s = vc<int>(n+1);
cnt = vc<int>(k+1);
g = in = vc<vc<int>>(n+1);
mp = vc<unordered_map<int, int>>(n+1);
fulq = queue<int>();
uq = queue<ii>();
for(int i=1; i<=n; i++){
cin >> t[i];
cnt[t[i]]++;
}
for(int i=1; i<=m; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for(int i=1; i<=n; i++){
uf[i] = i;
s[i] = 1 + (int)g[i].size();
in[i].pb(i);
if(cnt[t[i]] == 1) fulq.push(i);
}
for(int i=1; i<=n; i++){
for(auto j : g[i]){
if(t[i] == t[j]) uq.push({i, j});
}
}
while(1){
if(uq.size()){
U(uq.front().st, uq.front().nd);
uq.pop();
}
else if(fulq.size()){
full(fulq.front());
fulq.pop();
}
else break;
}
for(int i=1; i<=n; i++){
if((int)in[i].size() == n){
cout << "TAK\n";
return;
}
}
cout << "NIE\n";
return;
}
int main(){
BOOST;
int q;
cin >> q;
for(int i=1; i<=q; i++){
solve();
// printf("ok %d\n", i);
}
}
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define all(x) (x).begin(), (x).end() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) template<typename T> using vc = vector<T>; using ll = long long; using ld = long double; using ii = pair<int, int>; vc<int> t; vc<vc<int>> g; vc<vc<int>> in; vc<unordered_map<int, int>> mp; vc<int> mark; vc<int> cnt; vc<int> uf; vc<int> s; queue<int> fulq; queue<ii> uq; int F(int x){ if(uf[x] == x) return x; return uf[x] = F(uf[x]); } void U(int a, int b){ a = F(a), b = F(b); if(a == b) return; if(s[a] < s[b]) swap(a, b); // printf("merge %d, %d\n", a, b); uf[b] = a; s[a] += s[b]; for(auto x : in[b]) in[a].pb(x); if(mark[a] && mark[b]){ for(auto [party, xb] : mp[b]){ auto it = mp[a].find(party); if(it != mp[a].end()){ auto xa = (*it).nd; uq.push({xa, xb}); } else mp[a][party] = xb; } } if((int)in[a].size() == cnt[t[a]]) fulq.push(a); } // checked void full(int x){ x = F(x); // printf("full %d, kolor %d\n", x, t[x]); mark[x] = 1; for(auto it : in[x]){ for(auto jt : g[it]){ int y = F(jt); // printf("full %d, node %d, sasiad %d, node %d\n", x, it, y, jt); if(mark[y]){ uq.push({x, y}); continue; } // printf("alive x = %d, y = %d, t[%d] = %d\n", x, y, y, t[y]); auto xd = mp[x].find(t[y]); if(xd == mp[x].end()) mp[x][t[y]] = y; else uq.push({(*xd).nd, y}); } } } void solve(){ int n, m, k; cin >> n >> m >> k; t = mark = uf = s = vc<int>(n+1); cnt = vc<int>(k+1); g = in = vc<vc<int>>(n+1); mp = vc<unordered_map<int, int>>(n+1); fulq = queue<int>(); uq = queue<ii>(); for(int i=1; i<=n; i++){ cin >> t[i]; cnt[t[i]]++; } for(int i=1; i<=m; i++){ int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } for(int i=1; i<=n; i++){ uf[i] = i; s[i] = 1 + (int)g[i].size(); in[i].pb(i); if(cnt[t[i]] == 1) fulq.push(i); } for(int i=1; i<=n; i++){ for(auto j : g[i]){ if(t[i] == t[j]) uq.push({i, j}); } } while(1){ if(uq.size()){ U(uq.front().st, uq.front().nd); uq.pop(); } else if(fulq.size()){ full(fulq.front()); fulq.pop(); } else break; } for(int i=1; i<=n; i++){ if((int)in[i].size() == n){ cout << "TAK\n"; return; } } cout << "NIE\n"; return; } int main(){ BOOST; int q; cin >> q; for(int i=1; i<=q; i++){ solve(); // printf("ok %d\n", i); } } |
English