#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
#ifdef LOCAL
#define DTP(x, y) \
auto operator<<(auto &o, auto a)->decltype(y, o) \
{ \
o << "("; \
x; \
return o << ")"; \
}
DTP(o << a.first << ", " << a.second, a.second);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; }
#define debug(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define debug(...) 42
#endif
stringstream os;
void testing()
{
os = stringstream();
/*
Here create tests by pushing to os (<<).
*/
os.seekg(0, ios::beg);
cin.rdbuf(os.rdbuf());
}
struct UF
{
vi P;
vector<set<pii>> S; // 1 - kolor, 2-reprezentant
vi V, C;
vector<vi> U; // Wierchołki w danym kolorze
vector<vi> G; // Graf
queue<int> Q; // elementy gotowe
UF(int n, int k)
{
P.resize(n, -1); // Jeżeli P[i]=-INT_MAX to to jest joker
S.resize(n);
C.resize(n);
G.resize(n);
V.resize(k, 0);
U.resize(k);
}
int find(int x)
{
if (P[x] < 0)
return x;
P[x] = find(P[x]);
return P[x];
}
void join_c(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
return;
if (P[y] < P[x])
swap(x, y);
P[x] += P[y];
P[y] = x;
if (P[x] == -V[C[x]])
Q.push(x);
}
void join_j(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
return;
if (sz(S[x]) < sz(S[y]))
swap(x, y);
for (auto &[a, b] : S[y])
{
auto it = S[x].lower_bound({a, 0});
if (it != S[x].end() && (*it).first == a)
join_c((*it).second, b);
else
S[x].insert({a, b});
}
S[y].clear();
P[y] = x;
}
void upd()
{
int r = Q.front();
Q.pop();
// debug(r);
V[C[r]] = -1;
P[r] = -INT_MAX;
for (auto &i : U[C[r]])
for (auto &a : G[i])
{
if (P[find(a)] == -INT_MAX)
{
join_j(r, find(a));
r = find(r);
}
else
{
auto it = S[r].lower_bound({C[a], 0});
if (it != S[r].end() && (*it).first == C[a])
join_c(a, (*it).second);
else
S[r].insert({C[a], a});
}
}
}
};
void solve()
{
int n, m, k;
cin >> n >> m >> k;
UF X(n, k);
for (int i = 0; i < n; i++)
{
int c;
cin >> c;
--c;
X.C[i] = c;
X.U[c].push_back(i);
X.V[c]++;
}
for (int i = 0; i < n; i++)
if (X.V[X.C[i]] == 1)
X.Q.push(i);
while (m--)
{
int a, b;
cin >> a >> b;
--a;
--b;
if (X.C[a] == X.C[b])
X.join_c(a, b);
else
{
X.G[a].push_back(b);
X.G[b].push_back(a);
}
}
while (X.Q.size())
X.upd();
for (auto &a : X.V)
if (a > 0)
{
cout << "NIE\n";
return;
}
cout << "TAK\n";
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int Z = 1;
cin >> Z;
while (Z--)
solve();
}
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 | #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define YES cout << "YES\n" #define NO cout << "NO\n" typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef long double ld; #ifdef LOCAL #define DTP(x, y) \ auto operator<<(auto &o, auto a)->decltype(y, o) \ { \ o << "("; \ x; \ return o << ")"; \ } DTP(o << a.first << ", " << a.second, a.second); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define debug(...) 42 #endif stringstream os; void testing() { os = stringstream(); /* Here create tests by pushing to os (<<). */ os.seekg(0, ios::beg); cin.rdbuf(os.rdbuf()); } struct UF { vi P; vector<set<pii>> S; // 1 - kolor, 2-reprezentant vi V, C; vector<vi> U; // Wierchołki w danym kolorze vector<vi> G; // Graf queue<int> Q; // elementy gotowe UF(int n, int k) { P.resize(n, -1); // Jeżeli P[i]=-INT_MAX to to jest joker S.resize(n); C.resize(n); G.resize(n); V.resize(k, 0); U.resize(k); } int find(int x) { if (P[x] < 0) return x; P[x] = find(P[x]); return P[x]; } void join_c(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (P[y] < P[x]) swap(x, y); P[x] += P[y]; P[y] = x; if (P[x] == -V[C[x]]) Q.push(x); } void join_j(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sz(S[x]) < sz(S[y])) swap(x, y); for (auto &[a, b] : S[y]) { auto it = S[x].lower_bound({a, 0}); if (it != S[x].end() && (*it).first == a) join_c((*it).second, b); else S[x].insert({a, b}); } S[y].clear(); P[y] = x; } void upd() { int r = Q.front(); Q.pop(); // debug(r); V[C[r]] = -1; P[r] = -INT_MAX; for (auto &i : U[C[r]]) for (auto &a : G[i]) { if (P[find(a)] == -INT_MAX) { join_j(r, find(a)); r = find(r); } else { auto it = S[r].lower_bound({C[a], 0}); if (it != S[r].end() && (*it).first == C[a]) join_c(a, (*it).second); else S[r].insert({C[a], a}); } } } }; void solve() { int n, m, k; cin >> n >> m >> k; UF X(n, k); for (int i = 0; i < n; i++) { int c; cin >> c; --c; X.C[i] = c; X.U[c].push_back(i); X.V[c]++; } for (int i = 0; i < n; i++) if (X.V[X.C[i]] == 1) X.Q.push(i); while (m--) { int a, b; cin >> a >> b; --a; --b; if (X.C[a] == X.C[b]) X.join_c(a, b); else { X.G[a].push_back(b); X.G[b].push_back(a); } } while (X.Q.size()) X.upd(); for (auto &a : X.V) if (a > 0) { cout << "NIE\n"; return; } cout << "TAK\n"; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int Z = 1; cin >> Z; while (Z--) solve(); } |
English