/*
=-@%+-=+=+.#%-=**-==++============+===++==========-==#=.-.:@#++*+===----=---==--=-------
=:%+=%**++**%.-*+-++===============================+*#.-:.*@**#*+==------==-------------
===@+:-#-%+*+-=*+-==================++==============+@ -..@#++*+=++=-----==------==-----
==:-@#:#%=+=:==*+-========+======--::.:--=---::::.: :#.:..@****+==+=------=------------=
=##:.***-.:-#:=*+-========---::.:+*#%@###%%%%@%%%%@@@# +@****+====--------------------
-**#*#+*%*+%%.=*+-==-====-: :+#@@#+**#%%%***++=+++=+#@@@:+#***+++=-------=--------------
-=@=++*+-*#+@:=*+-=====-.:+@@%#++#*+*-....:-==++++##@@@@@@---++++=-------=--=-----------
*:@#=*=+*%+*=-=*+====---*@@%#+=*+--=*#@@@@@@@@@@@@@@@@@@@@@@*-::===--------=--=---------
-:.%#*-%@-*=.=+++=+=--#%*=-=*#%@@@@@@#*++=----==--++*#%%%#%%@@@= -------=---------------
:@=. *#+..:-%-=++--:=#+-+@@@@@%*+----=+++=++++++++++*+#*+#@%@@@@*.------=-=------=------
.@#*+%-*%=#%@:+*-..#%-+%@@%*===+++++++***++++*++++++++=*###@##%%#-.--=--=-=------==----=
:#+=*-*++#*%:-++::@@#*@@@#+++***++++++*+++=+++++++++++++*###*-%*@# :----=-=-------------
-:@+--*=#**%:=*+.*@%**@@#++*+*+++++++++++++++++++++++*+++*+#**%+%*..--=----------------=
=-:%*:+%#*%-:=*=-@@*=#@#**+*+++==+++++=+++===+==+=+++++++***%%**%@* :---=--------------=
:#.:++#=:::*+=#=:@@+#@##+++++*+++==+++=========+++++++++****+#%#*%%..------------------=
:@+=#=*+%=@@+=%-:@@##%*##*++++***+=====+==++++++++++++++****+##**%@# :-----=------------
.@=+=+*-+%%@.=%--@%@%###****+=====+==+++++++++=+===+++**#**++*###*@@. ..--=---------=--=
:*#=+-*+%=#%-=#--@%+*%*#****++++++++=+==++===+=++++++++**+*###*+%%#=.+- :----------=--=
-:*%+.#@-@*--+#==@#%###*+**#**+++=++==+=++++++=*+=----==**+++*##**%@@@@@+.-==----------=
+-.-**#.--.*-== :@#***##***+=--===+===========-+:.:-==-:=++++#%##%@@%+*@+.-----=-==-----
@@=*%:+%==@@.=@@%%*###%***--..:....:.::=++=--. *@@@@@@@#+**##+*#*--*#@+.-------==----=
*#-*-*+:*#@#.+@@@@#**%@***@@@@@@@@#+=+++++**+*@@@@%-.-*%@@****#*=%@#+##@-.--------=----=
:@=-++=*#-@:--***=%%*#@#*%@@*- +@@@@@*=+#@@@@:: -:=###**##+#%#*#@+ :-------==----=
=-@*.*##:@==+++*#+=#+=%*=*#=-=%@@@@@@@@#=++##@@@@@@@@@@@%#==+**%#=:#*+@=.--------==-----
=. #*##:+::=-++=@#%@@**##*@@@@=@@. :. %@-:-+# ::. -:-::#=+***%#=**+@@-.-----===------=
+=@# +.%#=+=-*+:@*=+*@@*+++..:. .:+%%#*%-:-%#=@@%*+-=+#**+=+=*##****%@+.:-----======---=
=::*-@%%--===%*.#@===*@*+++**@@@@@@===+@===%*-=:-@@@@@%+==+*+##*#%==+@+ --------===-----
:.=@@+.-=++=+##:=%%%#*%#*#*=----:::=++#%+::%#+**-. ..::-+****#***#*=+%-.-=--==--===---==
*%@@.-=++==+#*+=+*#+=+@#**##*====+=+***#+==*%*==--==-===++++****#*+%@*.-----========----
@@-:-+++-=+##+-==+*+-+@#*++=====-=:=#%%*=+++*%#%*-:-===-=+*#**##@@@@. :-=-------===--:-:
=:=++++=+#*+=-===#%=+%#***+==-::+**==*=.:::=#=*%@%#====+*******@- .---=------====---. :
=++*++--**@-+*##*****@@%##*+=+=%@@@@%++-.. .-+.+@=#@%+********##@# -------=======-=--. @%
+=--=+##@+--++**++*+:+@######%@@#==+#@@@@@@@@@@@#=-#@#*++*****##@# -==---====-======. @@*
=*++*@%---====+***+++=#@*%@*+#@#**++===+#@@%*+-++++=+###*+*#***%@* --------=-====--: @@@+
@@#*=-:==+++=+++**+++=@@**#*+##+====---:-+*+==-=+++=-=*#*+*#**#@- -==--=--====++=. %@%%+
-::-===+=++++++++****==%@##*=+##@@@@@@@#. --+%@@###*++*#**%%@# ---=----=+==. @@#%@+
+++++++++++++++++****+=#@@#%+-=##*+**#@@@@@@@@@@@@#@@#+==+#**%@*#@@ :---=--==-. @@=#%%+
=====+=====++++++*****+--####=++=-+%*=: .:+--:++=+%#+%@@- #@@@ -==--=+: @@*+##%+
========++++++++++****+:.@@%@@++*++*@@@@@@@@@@@@@+-=+*#%%%*%%@#:==@@@@@ .==--: @@@*=*%@+
+==++==+++*++++=+**+:. @@-@@%#*+*#*+#@%#%%%%%*++*****+**+#@@#:=:.#@@@@@= .-. @@%%#**%@
=====++*++++**= #@@@@# .@@@%+-====::------++==+===%@+@@@%-== -@@@%%@@@: @@@%%#+%@@
=+=++*+++=: .@@@@@@@@@#...@@@@#===++++==+==------#@#%@@@.:--. *@@@@@%@@@@@@###+#%#@*
=++=-:. +@@@@@@@@%%@@@@*.:..*@@@@*==--==+=-:+-==*@%%@@@=.-==- :@@@@@@@@@@@@@@@##:=*. .
- +=@@@@@@@@@@@%%@@@@@%===- .@@@@@%##*+**+*#@@@@@@@%..-=*=- #@@@@@@@@@@@@@@@@@@@*
..@@@@@@@@@@@@@@%%@@@@@@@@%*#*=- .@@@@@@@@@@@@@@@@@@- :==+++=. *@@%@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@%%%@@@@@@@@@*###*+--:. #@@+%@##@@@@= :.:++++++=: -@@@@@@@@@@@@@@@@@@@@@@@@@@
#@@@@@@@@@@@@@@@@@@@@@@@@%-+++##+==+. .:=+@-. ..:-+++=====-: :@@%%@@@%@@@@@@@@@@@@%@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@%:+==***+- @#@@@@@%@ +-+=======- .@@@@@@@@@@@@@@@@%%%%%@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@#.---++*=.+@@@@%.*@@@@@ --==----- @@@@@%@@@@@@@@@@@@@@@@@@%@@@*
#@@@@@@@@@@@@@@@@@@@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 - @@@@@@@@@@%@@@@@@%%@@@@%@@@@@@@@@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@= +- @@@*@@@@:@@@%=:.. @@@@@@@%@@@@@@@@@%%@@@%@@@@@@@@@@@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@: =. @@@@@ =@@@ +@@@ @@@@@@@@@@@@@@@@@%@@@@@@@@@@@@@@@@@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@* . @@@=*@@@*@@#@@@# @@@@@@@@@@@%@@@%%@@@@@%@@@@@@@@@@@@@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@@. @@@+%@@@.-@@@:%- @@@%@@@@@@@@@@@@@@@@@%@@@@@@@@@@@@@@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@@= @@=@@@. @@@* @@@#@@@@@@@@@@@@@@%@@@@%%@@@@@@@@@@@@@@@@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@@. @@@@+@@@@+@@@@%@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%*
#@@@@@@@@@@@@@@%@@@@@@@@@@@@ .@@@%*@%@@ @@@@ @%@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@@@@@@@@@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@@-@@ *@@* *@@@ @@@%@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@%%%@@*
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@=@@@@@@@@@@@%%@@@@@@@@%@@@@@@@%@@@@@@@@@@@@@@@%%%%@@@@@@#
#@@@@@@@@@@@@@@@@@@@@@@@@@@@%@@@@-#@@@.+@@@@@@@@@@@@@@@@@@@@@%@@@@@@@@@@@@@@@%@@@@@%@@@@@#
*###########***##*##########*@+ +%# *##- %************#*********************#**######%##*
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(a) (int)a.size()
#define pb push_back
#define st first
#define nd second
#define endl '\n'
#define fast ios_base::sync_with_stdio(NULL);cin.tie(0); cout.tie(0)
#define debug(x) cout << #x << " = " << x << endl;
#define vc vector
#define pii pair<int,int>
#define pll pair<ll,ll>
struct customhsh
{
static ull splitmix(ull x)
{
x+=21372137ull;
x+=(x^(x>>30))*67676767ull;
x=(x^(x>>27))*67676969ull;
return x^(x>>31);
}
size_t operator()(ull x)const
{
static const ull fixed=chrono::steady_clock::now().time_since_epoch().count();
return splitmix(x+fixed);
}
};
void solve()
{
int n,m,k;
cin >> n >> m >> k;
vc<int> a(n);
for(int i=0; i<n; ++i)
{
cin >> a[i];
--a[i];
}
vc<vc<int>> g(n);
vc<pii> e;
for(int i=0; i<m; ++i)
{
int u,v;
cin >> u >> v;
--u;
--v;
g[u].pb(v);
g[v].pb(u);
e.pb({u,v});
}
vc<int> cid(n,-1);
vc<int> ccol;
vc<vc<int>> colnodes(k);
vc<int> sot;
int comps=0;
for(int i=0; i<n; ++i)
{
if(cid[i]==-1)
{
int c=a[i];
cid[i]=comps;
sot.clear();
sot.pb(i);
while(!sot.empty())
{
int v=sot.back();
sot.pop_back();
for(int x:g[v])
{
if(cid[x]==-1 and a[x]==c)
{
cid[x]=comps;
sot.pb(x);
}
}
}
ccol.pb(c);
colnodes[c].pb(comps);
comps++;
}
}
vc<vc<int>> cadj(comps);
for(auto [u,v]:e)
{
int x=cid[u];
int y=cid[v];
if(x==y)
continue;
cadj[x].pb(y);
cadj[y].pb(x);
}
vc<int> cnt(k,0);
vc<bool> actcol(k,0);
int need=0;
for(int c=0; c<k; ++c)
{
if(!colnodes[c].empty())
{
cnt[c]=sz(colnodes[c]);
need++;
}
}
vc<int> lpar(comps);
vc<int> lsz(comps,1);
iota(all(lpar),0);
auto lfind=[&](int x)
{
while(lpar[x]!=x)
{
lpar[x]=lpar[lpar[x]];
x=lpar[x];
}
return x;
};
auto lunion=[&](int x, int y, int c)
{
x=lfind(x);
y=lfind(y);
if(x==y)
return 0;
if(lsz[x]<lsz[y])
swap(x,y);
lpar[y]=x;
lsz[x]+=lsz[y];
cnt[c]--;
return 1;
};
vc<int> gpar(comps);
vc<int> gsz(comps,1);
iota(all(gpar),0);
vc<unordered_map<int,int,customhsh>> mp(comps);
deque<int> q;
auto gfind=[&](int x)
{
while(gpar[x]!=x)
{
gpar[x]=gpar[gpar[x]];
x=gpar[x];
}
return x;
};
auto tpush=[&](int c)
{
if(!actcol[c] and cnt[c]==1)
q.pb(c);
};
auto gunion=[&](int x, int y)
{
x=gfind(x);
y=gfind(y);
if(x==y)
return;
if(sz(mp[x])<sz(mp[y]))
swap(x,y);
gpar[y]=x;
gsz[x]+=gsz[y];
for(auto &f:mp[y])
{
int c=f.st;
if(actcol[c])
continue;
int rep=lfind(f.nd);
auto it=mp[x].find(c);
if(it==mp[x].end())
mp[x][c]=rep;
else
{
if(lunion(it->nd,rep,c))
tpush(c);
it->nd=lfind(it->nd);
}
}
mp[y].clear();
};
for(int c=0; c<k; ++c)
if(cnt[c]==1)
q.pb(c);
int done=0;
while(!q.empty())
{
int c=q.front();
q.pop_front();
if(actcol[c] or cnt[c]!=1)
continue;
actcol[c]=1;
done++;
for(int u:colnodes[c])
{
gpar[u]=u;
gsz[u]=1;
mp[u].clear();
for(int v:cadj[u])
{
int cc=ccol[v];
if(actcol[cc])
continue;
int rep=lfind(v);
auto it=mp[u].find(cc);
if(it==mp[u].end())
mp[u][cc]=rep;
else
{
if(lunion(it->nd,rep,cc))
tpush(cc);
it->nd=lfind(it->nd);
}
}
for(int v:cadj[u])
{
if(actcol[ccol[v]])
gunion(u,v);
}
}
}
cout << (done==need? "TAK":"NIE") << endl;
}
int main()
{
fast;
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | /* =-@%+-=+=+.#%-=**-==++============+===++==========-==#=.-.:@#++*+===----=---==--=------- =:%+=%**++**%.-*+-++===============================+*#.-:.*@**#*+==------==------------- ===@+:-#-%+*+-=*+-==================++==============+@ -..@#++*+=++=-----==------==----- ==:-@#:#%=+=:==*+-========+======--::.:--=---::::.: :#.:..@****+==+=------=------------= =##:.***-.:-#:=*+-========---::.:+*#%@###%%%%@%%%%@@@# +@****+====-------------------- -**#*#+*%*+%%.=*+-==-====-: :+#@@#+**#%%%***++=+++=+#@@@:+#***+++=-------=-------------- -=@=++*+-*#+@:=*+-=====-.:+@@%#++#*+*-....:-==++++##@@@@@@---++++=-------=--=----------- *:@#=*=+*%+*=-=*+====---*@@%#+=*+--=*#@@@@@@@@@@@@@@@@@@@@@@*-::===--------=--=--------- -:.%#*-%@-*=.=+++=+=--#%*=-=*#%@@@@@@#*++=----==--++*#%%%#%%@@@= -------=--------------- :@=. *#+..:-%-=++--:=#+-+@@@@@%*+----=+++=++++++++++*+#*+#@%@@@@*.------=-=------=------ .@#*+%-*%=#%@:+*-..#%-+%@@%*===+++++++***++++*++++++++=*###@##%%#-.--=--=-=------==----= :#+=*-*++#*%:-++::@@#*@@@#+++***++++++*+++=+++++++++++++*###*-%*@# :----=-=------------- -:@+--*=#**%:=*+.*@%**@@#++*+*+++++++++++++++++++++++*+++*+#**%+%*..--=----------------= =-:%*:+%#*%-:=*=-@@*=#@#**+*+++==+++++=+++===+==+=+++++++***%%**%@* :---=--------------= :#.:++#=:::*+=#=:@@+#@##+++++*+++==+++=========+++++++++****+#%#*%%..------------------= :@+=#=*+%=@@+=%-:@@##%*##*++++***+=====+==++++++++++++++****+##**%@# :-----=------------ .@=+=+*-+%%@.=%--@%@%###****+=====+==+++++++++=+===+++**#**++*###*@@. ..--=---------=--= :*#=+-*+%=#%-=#--@%+*%*#****++++++++=+==++===+=++++++++**+*###*+%%#=.+- :----------=--= -:*%+.#@-@*--+#==@#%###*+**#**+++=++==+=++++++=*+=----==**+++*##**%@@@@@+.-==----------= +-.-**#.--.*-== :@#***##***+=--===+===========-+:.:-==-:=++++#%##%@@%+*@+.-----=-==----- @@=*%:+%==@@.=@@%%*###%***--..:....:.::=++=--. *@@@@@@@#+**##+*#*--*#@+.-------==----= *#-*-*+:*#@#.+@@@@#**%@***@@@@@@@@#+=+++++**+*@@@@%-.-*%@@****#*=%@#+##@-.--------=----= :@=-++=*#-@:--***=%%*#@#*%@@*- +@@@@@*=+#@@@@:: -:=###**##+#%#*#@+ :-------==----= =-@*.*##:@==+++*#+=#+=%*=*#=-=%@@@@@@@@#=++##@@@@@@@@@@@%#==+**%#=:#*+@=.--------==----- =. #*##:+::=-++=@#%@@**##*@@@@=@@. :. %@-:-+# ::. -:-::#=+***%#=**+@@-.-----===------= +=@# +.%#=+=-*+:@*=+*@@*+++..:. .:+%%#*%-:-%#=@@%*+-=+#**+=+=*##****%@+.:-----======---= =::*-@%%--===%*.#@===*@*+++**@@@@@@===+@===%*-=:-@@@@@%+==+*+##*#%==+@+ --------===----- :.=@@+.-=++=+##:=%%%#*%#*#*=----:::=++#%+::%#+**-. ..::-+****#***#*=+%-.-=--==--===---== *%@@.-=++==+#*+=+*#+=+@#**##*====+=+***#+==*%*==--==-===++++****#*+%@*.-----========---- @@-:-+++-=+##+-==+*+-+@#*++=====-=:=#%%*=+++*%#%*-:-===-=+*#**##@@@@. :-=-------===--:-: =:=++++=+#*+=-===#%=+%#***+==-::+**==*=.:::=#=*%@%#====+*******@- .---=------====---. : =++*++--**@-+*##*****@@%##*+=+=%@@@@%++-.. .-+.+@=#@%+********##@# -------=======-=--. @% +=--=+##@+--++**++*+:+@######%@@#==+#@@@@@@@@@@@#=-#@#*++*****##@# -==---====-======. @@* =*++*@%---====+***+++=#@*%@*+#@#**++===+#@@%*+-++++=+###*+*#***%@* --------=-====--: @@@+ @@#*=-:==+++=+++**+++=@@**#*+##+====---:-+*+==-=+++=-=*#*+*#**#@- -==--=--====++=. %@%%+ -::-===+=++++++++****==%@##*=+##@@@@@@@#. --+%@@###*++*#**%%@# ---=----=+==. @@#%@+ +++++++++++++++++****+=#@@#%+-=##*+**#@@@@@@@@@@@@#@@#+==+#**%@*#@@ :---=--==-. @@=#%%+ =====+=====++++++*****+--####=++=-+%*=: .:+--:++=+%#+%@@- #@@@ -==--=+: @@*+##%+ ========++++++++++****+:.@@%@@++*++*@@@@@@@@@@@@@+-=+*#%%%*%%@#:==@@@@@ .==--: @@@*=*%@+ +==++==+++*++++=+**+:. @@-@@%#*+*#*+#@%#%%%%%*++*****+**+#@@#:=:.#@@@@@= .-. @@%%#**%@ =====++*++++**= #@@@@# .@@@%+-====::------++==+===%@+@@@%-== -@@@%%@@@: @@@%%#+%@@ =+=++*+++=: .@@@@@@@@@#...@@@@#===++++==+==------#@#%@@@.:--. *@@@@@%@@@@@@###+#%#@* =++=-:. +@@@@@@@@%%@@@@*.:..*@@@@*==--==+=-:+-==*@%%@@@=.-==- :@@@@@@@@@@@@@@@##:=*. . - +=@@@@@@@@@@@%%@@@@@%===- .@@@@@%##*+**+*#@@@@@@@%..-=*=- #@@@@@@@@@@@@@@@@@@@* ..@@@@@@@@@@@@@@%%@@@@@@@@%*#*=- .@@@@@@@@@@@@@@@@@@- :==+++=. *@@%@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@%%%@@@@@@@@@*###*+--:. #@@+%@##@@@@= :.:++++++=: -@@@@@@@@@@@@@@@@@@@@@@@@@@ #@@@@@@@@@@@@@@@@@@@@@@@@%-+++##+==+. .:=+@-. ..:-+++=====-: :@@%%@@@%@@@@@@@@@@@@%@@@@@* #@@@@@@@@@@@@@@@@@@@@@@@@%:+==***+- @#@@@@@%@ +-+=======- .@@@@@@@@@@@@@@@@%%%%%@@@@@@* #@@@@@@@@@@@@@@@@@@@@@@@@#.---++*=.+@@@@%.*@@@@@ --==----- @@@@@%@@@@@@@@@@@@@@@@@@%@@@* #@@@@@@@@@@@@@@@@@@@@@@@@%:--==++.@@#@=@@@@* @@@@@ .-==-=-. #@@@@@@@@@@@@@@@@@@@%%%@@@@@@* #@@@@@@@@@@@@@@@@@@@@@@@@#:---===%@#.@@@=.@@@@@ .%@% :===: =@@@%@@@@@@@@@@@@%%%%%@@@@%@@@* #@@@@@@@@@@@@@@@@@@@@@@@@@=----+@%: -@@%@@@@ ::-%@+-:: @@@%@@@@@@@@@@@@@@@@@@@@@@@@@@# #@@@@@@@@@@@@@@@@@@@@@@@@@=.-=%*-:+* @@@*@@* :---=::-%= @@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@* #@@@@@@@@@@@@@@@@@@@@@@@@@==+::--==+ =@@@#@ +:++==---: @@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@* #@@@@@@@@@@@@@@@@@@@@@@@@@+:=====--: @: %@@@ +:=----=-. @@@%%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@%%@@@@@@@@@@@@@@@@@@@@@* #@@@@@@@@@@@@@@@@@@@@@@@@@@@. @@@@+@@@@+@@@@%@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%* #@@@@@@@@@@@@@@%@@@@@@@@@@@@ .@@@%*@%@@ @@@@ @%@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@@@@@@@@@@* #@@@@@@@@@@@@@@@@@@@@@@@@@@@-@@ *@@* *@@@ @@@%@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@%%%@@* #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@=@@@@@@@@@@@%%@@@@@@@@%@@@@@@@%@@@@@@@@@@@@@@@%%%%@@@@@@# #@@@@@@@@@@@@@@@@@@@@@@@@@@@%@@@@-#@@@.+@@@@@@@@@@@@@@@@@@@@@%@@@@@@@@@@@@@@@%@@@@@%@@@@@# *###########***##*##########*@+ +%# *##- %************#*********************#**######%##* */ #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define sz(a) (int)a.size() #define pb push_back #define st first #define nd second #define endl '\n' #define fast ios_base::sync_with_stdio(NULL);cin.tie(0); cout.tie(0) #define debug(x) cout << #x << " = " << x << endl; #define vc vector #define pii pair<int,int> #define pll pair<ll,ll> struct customhsh { static ull splitmix(ull x) { x+=21372137ull; x+=(x^(x>>30))*67676767ull; x=(x^(x>>27))*67676969ull; return x^(x>>31); } size_t operator()(ull x)const { static const ull fixed=chrono::steady_clock::now().time_since_epoch().count(); return splitmix(x+fixed); } }; void solve() { int n,m,k; cin >> n >> m >> k; vc<int> a(n); for(int i=0; i<n; ++i) { cin >> a[i]; --a[i]; } vc<vc<int>> g(n); vc<pii> e; for(int i=0; i<m; ++i) { int u,v; cin >> u >> v; --u; --v; g[u].pb(v); g[v].pb(u); e.pb({u,v}); } vc<int> cid(n,-1); vc<int> ccol; vc<vc<int>> colnodes(k); vc<int> sot; int comps=0; for(int i=0; i<n; ++i) { if(cid[i]==-1) { int c=a[i]; cid[i]=comps; sot.clear(); sot.pb(i); while(!sot.empty()) { int v=sot.back(); sot.pop_back(); for(int x:g[v]) { if(cid[x]==-1 and a[x]==c) { cid[x]=comps; sot.pb(x); } } } ccol.pb(c); colnodes[c].pb(comps); comps++; } } vc<vc<int>> cadj(comps); for(auto [u,v]:e) { int x=cid[u]; int y=cid[v]; if(x==y) continue; cadj[x].pb(y); cadj[y].pb(x); } vc<int> cnt(k,0); vc<bool> actcol(k,0); int need=0; for(int c=0; c<k; ++c) { if(!colnodes[c].empty()) { cnt[c]=sz(colnodes[c]); need++; } } vc<int> lpar(comps); vc<int> lsz(comps,1); iota(all(lpar),0); auto lfind=[&](int x) { while(lpar[x]!=x) { lpar[x]=lpar[lpar[x]]; x=lpar[x]; } return x; }; auto lunion=[&](int x, int y, int c) { x=lfind(x); y=lfind(y); if(x==y) return 0; if(lsz[x]<lsz[y]) swap(x,y); lpar[y]=x; lsz[x]+=lsz[y]; cnt[c]--; return 1; }; vc<int> gpar(comps); vc<int> gsz(comps,1); iota(all(gpar),0); vc<unordered_map<int,int,customhsh>> mp(comps); deque<int> q; auto gfind=[&](int x) { while(gpar[x]!=x) { gpar[x]=gpar[gpar[x]]; x=gpar[x]; } return x; }; auto tpush=[&](int c) { if(!actcol[c] and cnt[c]==1) q.pb(c); }; auto gunion=[&](int x, int y) { x=gfind(x); y=gfind(y); if(x==y) return; if(sz(mp[x])<sz(mp[y])) swap(x,y); gpar[y]=x; gsz[x]+=gsz[y]; for(auto &f:mp[y]) { int c=f.st; if(actcol[c]) continue; int rep=lfind(f.nd); auto it=mp[x].find(c); if(it==mp[x].end()) mp[x][c]=rep; else { if(lunion(it->nd,rep,c)) tpush(c); it->nd=lfind(it->nd); } } mp[y].clear(); }; for(int c=0; c<k; ++c) if(cnt[c]==1) q.pb(c); int done=0; while(!q.empty()) { int c=q.front(); q.pop_front(); if(actcol[c] or cnt[c]!=1) continue; actcol[c]=1; done++; for(int u:colnodes[c]) { gpar[u]=u; gsz[u]=1; mp[u].clear(); for(int v:cadj[u]) { int cc=ccol[v]; if(actcol[cc]) continue; int rep=lfind(v); auto it=mp[u].find(cc); if(it==mp[u].end()) mp[u][cc]=rep; else { if(lunion(it->nd,rep,cc)) tpush(cc); it->nd=lfind(it->nd); } } for(int v:cadj[u]) { if(actcol[ccol[v]]) gunion(u,v); } } } cout << (done==need? "TAK":"NIE") << endl; } int main() { fast; int t=1; cin >> t; while(t--) solve(); return 0; } |
English