#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#else
#define debug(...) 228
#endif
typedef long long ll;
typedef long double ld;
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
vector<int> read_vec(int n) {
vector<int> v(n);
for (int& t : v) cin >> t;
return v;
}
template<typename T>
inline void upd_max(T& a, T b) {
if (a < b) {
a = b;
}
}
template<typename T>
inline void upd_min(T& a, T b) {
if (a > b) {
a = b;
}
}
int n, m, k;
//для 0 и 1 индексации
struct dsu{
vector<int> p, rank;
dsu(int n) {
p = vector<int>(n + 1);
rank = vector<int>(n + 1);
for (int i = 0; i <= n; i++) {
p[i] = i;
rank[i] = 0;
}
}
int get(int a) {
if (a == p[a]) return a;
return p[a] = get(p[a]);
}
bool unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b) return false;
if (rank[a] < rank[b]) swap(a, b);
p[b] = a;
rank[a] += (rank[a] == rank[b]);
return true;
}
};
void solve() {
cin >> n >> m >> k;
vector<map<int,int>> st(n + 1);
dsu ds(n);
vector<int> cnt(k + 1, -1);
vector<int> a(n + 1);
vector<vector<int>> by(k + 1);
FOR(i, 1, n + 1) {
cin >> a[i];
cnt[a[i]]++;
by[a[i]].emplace_back(i);
}
vector<vector<int>> g(n + 1);
vector<int> fin(k + 1);
FOR(i, 1, m + 1) {
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
if (a[x] == a[y]) {
if (ds.unite(x, y)) {
cnt[a[x]]--;
}
}
}
queue<int> q;
FOR(i, 1, k + 1) {
if (cnt[i] == 0) {
fin[i] = 1;
q.push(i);
}
}
vector<int> is_free(n + 1);
dsu for_free(n);
while (!q.empty()) {
int clr = q.front();
q.pop();
for (int id: by[clr]) {
is_free[id] = 1;
for (int to: g[id]) {
if (!is_free[to] && !fin[a[to]]) {
if (!st[id].count(a[to])) {
st[id][a[to]] = to;
} else {
int who = st[id][a[to]];
if (ds.unite(who, to)) {
cnt[a[to]]--;
if (cnt[a[to]] == 0) {
fin[a[to]] = 1;
q.push(a[to]);
}
}
}
}
}
for (int to : g[id]) {
if (is_free[to]) {
int X = for_free.get(to);
int Y = for_free.get(id);
if (X != Y) {
if (st[X].size() < st[Y].size()) swap(st[X], st[Y]);
for (auto& [C, vert] : st[Y]) {
if (!fin[C]) {
if (!st[X].count(C)) {
st[X][C] = vert;
}
else {
if (ds.unite(st[X][C], vert)) {
cnt[C]--;
if (cnt[C] == 0) {
fin[C] = 1;
q.push(C);
}
}
}
}
}
for_free.unite(X, Y);
if (X != for_free.get(X)) {
assert(Y == for_free.get(X));
swap(st[X], st[Y]);
st[X].clear();
}
else {
st[Y].clear();
}
}
}
}
}
}
FOR(i, 1, k + 1) {
if (cnt[i] > 0) {
cout << "NIE\n";
return;
}
}
cout << "TAK\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int tst;
cin >> tst;
while (tst--) 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 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif typedef long long ll; typedef long double ld; #define pb push_back #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) vector<int> read_vec(int n) { vector<int> v(n); for (int& t : v) cin >> t; return v; } template<typename T> inline void upd_max(T& a, T b) { if (a < b) { a = b; } } template<typename T> inline void upd_min(T& a, T b) { if (a > b) { a = b; } } int n, m, k; //для 0 и 1 индексации struct dsu{ vector<int> p, rank; dsu(int n) { p = vector<int>(n + 1); rank = vector<int>(n + 1); for (int i = 0; i <= n; i++) { p[i] = i; rank[i] = 0; } } int get(int a) { if (a == p[a]) return a; return p[a] = get(p[a]); } bool unite(int a, int b) { a = get(a); b = get(b); if (a == b) return false; if (rank[a] < rank[b]) swap(a, b); p[b] = a; rank[a] += (rank[a] == rank[b]); return true; } }; void solve() { cin >> n >> m >> k; vector<map<int,int>> st(n + 1); dsu ds(n); vector<int> cnt(k + 1, -1); vector<int> a(n + 1); vector<vector<int>> by(k + 1); FOR(i, 1, n + 1) { cin >> a[i]; cnt[a[i]]++; by[a[i]].emplace_back(i); } vector<vector<int>> g(n + 1); vector<int> fin(k + 1); FOR(i, 1, m + 1) { int x, y; cin >> x >> y; g[x].pb(y); g[y].pb(x); if (a[x] == a[y]) { if (ds.unite(x, y)) { cnt[a[x]]--; } } } queue<int> q; FOR(i, 1, k + 1) { if (cnt[i] == 0) { fin[i] = 1; q.push(i); } } vector<int> is_free(n + 1); dsu for_free(n); while (!q.empty()) { int clr = q.front(); q.pop(); for (int id: by[clr]) { is_free[id] = 1; for (int to: g[id]) { if (!is_free[to] && !fin[a[to]]) { if (!st[id].count(a[to])) { st[id][a[to]] = to; } else { int who = st[id][a[to]]; if (ds.unite(who, to)) { cnt[a[to]]--; if (cnt[a[to]] == 0) { fin[a[to]] = 1; q.push(a[to]); } } } } } for (int to : g[id]) { if (is_free[to]) { int X = for_free.get(to); int Y = for_free.get(id); if (X != Y) { if (st[X].size() < st[Y].size()) swap(st[X], st[Y]); for (auto& [C, vert] : st[Y]) { if (!fin[C]) { if (!st[X].count(C)) { st[X][C] = vert; } else { if (ds.unite(st[X][C], vert)) { cnt[C]--; if (cnt[C] == 0) { fin[C] = 1; q.push(C); } } } } } for_free.unite(X, Y); if (X != for_free.get(X)) { assert(Y == for_free.get(X)); swap(st[X], st[Y]); st[X].clear(); } else { st[Y].clear(); } } } } } } FOR(i, 1, k + 1) { if (cnt[i] > 0) { cout << "NIE\n"; return; } } cout << "TAK\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int tst; cin >> tst; while (tst--) solve(); return 0; } |
English