//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;
using i128=__int128_t;
void die(string S){puts(S.c_str());exit(0);}
int n,m,k;
int cnt[100100];
int fa[100100],fa2[100100];
inline int anc(int x)
{
while(fa[x]!=x) x=fa[x]=fa[fa[x]];
return x;
}
inline int anc2(int x)
{
while(fa2[x]!=x) x=fa2[x]=fa2[fa2[x]];
return x;
}
map<int,int> Mp[100100];
vector<int> G[100100],vec[100100];
int col[100100],ban[100100];
queue<int> q;
void connect(int u,int v)
{
u=anc(u);
v=anc(v);
if(u==v) return ;
assert(col[u]==col[v]);
cnt[col[u]]--;
if(cnt[col[u]]==1) q.push(col[u]);
fa[u]=v;
}
void connect2(int u,int v)
{
u=anc2(u);
v=anc2(v);
if(u==v) return ;
if(sz(Mp[u])>sz(Mp[v])) swap(u,v);
fa2[u]=v;
for(auto [a,b]:Mp[u])
if(Mp[v][a])
connect(Mp[v][a],b);
else
Mp[v][a]=b;
map<int,int>().swap(Mp[u]);
}
void solve()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
G[i].clear();
for(int i=1;i<=n;i++)
fa[i]=fa2[i]=i;
for(int i=1;i<=k;i++)
{
cnt[i]=0;
vec[i].clear();
}
for(int i=1;i<=n;i++)
{
cin>>col[i];
ban[i]=0;
cnt[col[i]]++;
vec[col[i]].pb(i);
map<int,int>().swap(Mp[i]);
}
while(m--)
{
int u,v;
cin>>u>>v;
G[u].pb(v);
G[v].pb(u);
}
for(int i=1;i<=k;i++)
if(cnt[i]==1)
q.push(i);
for(int i=1;i<=n;i++)
for(auto j:G[i])
if(col[i]==col[j])
connect(i,j);
while(!q.empty())
{
int c=q.front();
q.pop();
for(auto x:vec[c])
{
ban[x]=1;
for(auto y:G[x])
if(Mp[x][col[y]])
connect(Mp[x][col[y]],y);
else
Mp[x][col[y]]=y;
for(auto y:G[x])
if(ban[y])
connect2(x,y);
}
}
for(int i=1;i<=k;i++)
if(cnt[i]>1)
{
cout<<"NIE\n";
return ;
}
cout<<"TAK\n";
}
int main()
{
ios_base::sync_with_stdio(false);
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 | //Author: Kevin #include<bits/stdc++.h> //#pragma GCC optimize("O2") using namespace std; #define pb emplace_back #define mp make_pair #define ALL(x) (x).begin(),(x).end() #define rALL(x) (x).rbegin(),(x).rend() #define srt(x) sort(ALL(x)) #define rev(x) reverse(ALL(x)) #define rsrt(x) sort(rALL(x)) #define sz(x) (int)(x.size()) #define inf 0x3f3f3f3f #define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin()) #define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin()) #define uni(v) v.resize(unique(ALL(v))-v.begin()) using ll=long long; using ull=unsigned long long; using pii=pair<int,int>; using i128=__int128_t; void die(string S){puts(S.c_str());exit(0);} int n,m,k; int cnt[100100]; int fa[100100],fa2[100100]; inline int anc(int x) { while(fa[x]!=x) x=fa[x]=fa[fa[x]]; return x; } inline int anc2(int x) { while(fa2[x]!=x) x=fa2[x]=fa2[fa2[x]]; return x; } map<int,int> Mp[100100]; vector<int> G[100100],vec[100100]; int col[100100],ban[100100]; queue<int> q; void connect(int u,int v) { u=anc(u); v=anc(v); if(u==v) return ; assert(col[u]==col[v]); cnt[col[u]]--; if(cnt[col[u]]==1) q.push(col[u]); fa[u]=v; } void connect2(int u,int v) { u=anc2(u); v=anc2(v); if(u==v) return ; if(sz(Mp[u])>sz(Mp[v])) swap(u,v); fa2[u]=v; for(auto [a,b]:Mp[u]) if(Mp[v][a]) connect(Mp[v][a],b); else Mp[v][a]=b; map<int,int>().swap(Mp[u]); } void solve() { cin>>n>>m>>k; for(int i=1;i<=n;i++) G[i].clear(); for(int i=1;i<=n;i++) fa[i]=fa2[i]=i; for(int i=1;i<=k;i++) { cnt[i]=0; vec[i].clear(); } for(int i=1;i<=n;i++) { cin>>col[i]; ban[i]=0; cnt[col[i]]++; vec[col[i]].pb(i); map<int,int>().swap(Mp[i]); } while(m--) { int u,v; cin>>u>>v; G[u].pb(v); G[v].pb(u); } for(int i=1;i<=k;i++) if(cnt[i]==1) q.push(i); for(int i=1;i<=n;i++) for(auto j:G[i]) if(col[i]==col[j]) connect(i,j); while(!q.empty()) { int c=q.front(); q.pop(); for(auto x:vec[c]) { ban[x]=1; for(auto y:G[x]) if(Mp[x][col[y]]) connect(Mp[x][col[y]],y); else Mp[x][col[y]]=y; for(auto y:G[x]) if(ban[y]) connect2(x,y); } } for(int i=1;i<=k;i++) if(cnt[i]>1) { cout<<"NIE\n"; return ; } cout<<"TAK\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) solve(); return 0; } |
English