/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graf;
vector<int>odw;
vector<bool> ac;
void dfs(int v){
odw[v]=1;
for(int s:graf[v]){
if(!odw[s] and ac[s]){
dfs(s);
}
}
}
int main()
{
int t;cin>>t;
for(int i=1;i<=t;i++){
int n,m,k;cin>>n>>m>>k;
graf.assign(n+1, {});
ac.assign(n+1, true);
vector<int> wyg(n+1);
for(int x=1;x<=n;x++){
cin>>wyg[x];
}
for(int x=1;x<=m;x++){
int a,b;cin>>a>>b;
graf[a].push_back(b);
graf[b].push_back(a);
}
bool z=true;
vector<int> us(k+1,0);
int ilu=0;
while(z){
z=false;
for(int c=1;c<=k;c++){
if(us[c]){
continue;
}
int st=-1;
for(int i=1;i<=n;i++){
if(ac[i] and wyg[i]==c){
st=i;
break;
}
}
if(st==-1){
z=true;
us[c]=true;
ilu++;
continue;
}
odw.assign(n+1, 0);
dfs(st);
bool ok=true;
for(int i=1;i<=n;i++){
if(ac[i] and wyg[i]==c and !odw[i]){
ok=false;
break;
}
}
if(ok){
for(int i=1;i<=n;i++){
if(ac[i] and wyg[i]==c){
ac[i]=false;
}
}
us[c]=true;
ilu++;
z=true;
}
}
}
if(ilu==k){
cout<<"TAK"<<endl;
}
else{
cout<<"NIE"<<endl;
}
}
}
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 | /****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; vector<vector<int>> graf; vector<int>odw; vector<bool> ac; void dfs(int v){ odw[v]=1; for(int s:graf[v]){ if(!odw[s] and ac[s]){ dfs(s); } } } int main() { int t;cin>>t; for(int i=1;i<=t;i++){ int n,m,k;cin>>n>>m>>k; graf.assign(n+1, {}); ac.assign(n+1, true); vector<int> wyg(n+1); for(int x=1;x<=n;x++){ cin>>wyg[x]; } for(int x=1;x<=m;x++){ int a,b;cin>>a>>b; graf[a].push_back(b); graf[b].push_back(a); } bool z=true; vector<int> us(k+1,0); int ilu=0; while(z){ z=false; for(int c=1;c<=k;c++){ if(us[c]){ continue; } int st=-1; for(int i=1;i<=n;i++){ if(ac[i] and wyg[i]==c){ st=i; break; } } if(st==-1){ z=true; us[c]=true; ilu++; continue; } odw.assign(n+1, 0); dfs(st); bool ok=true; for(int i=1;i<=n;i++){ if(ac[i] and wyg[i]==c and !odw[i]){ ok=false; break; } } if(ok){ for(int i=1;i<=n;i++){ if(ac[i] and wyg[i]==c){ ac[i]=false; } } us[c]=true; ilu++; z=true; } } } if(ilu==k){ cout<<"TAK"<<endl; } else{ cout<<"NIE"<<endl; } } } |
English