#include <bits/stdc++.h>
using namespace std;
int color[100009];
vector<int> graph[100009];
bool visited[100009];
vector<int> ofcol[100009];
vector<int> f;
int dfs(int node, int c) {
f.push_back(node);
visited[node] = true;
int s = 0;
if (color[node] == c) {
s = 1;
}
for (auto x: graph[node]) {
if ((color[x] == c or color[x] == 0) and !visited[x]) {
s += dfs(x,c);
}
}
return s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--) {
int n,m,k;
cin>>n>>m>>k;
for (int i = 1; i<=k; i++) {
ofcol[i].clear();
}
for (int i = 1; i<=n; i++) {
cin>>color[i];
graph[i].clear();
ofcol[color[i]].push_back(i);
visited[i] = false;
}
for (int i = 0; i<m; i++) {
int a,b;
cin>>a>>b;
graph[a].push_back(b);
graph[b].push_back(a);
}
unordered_set<int> left;
for (int i = 1; i<=k; i++) {
left.insert(i);
}
bool sigma = true;
while (!left.empty()) {
int g = -1;
for (auto x: left){
int ile = 0;
if (ofcol[x].size() != 0) {
f.clear();
ile = dfs(ofcol[x][0],x);
for (auto q: f) {
visited[q] = false;
}
}
if (ile == ofcol[x].size()) {
g = x;
left.erase(x);
break;
}
}
if (g != -1) {
for (auto x: ofcol[g]) {
color[x] = 0;
}
}else {
cout << "NIE\n";
sigma = false;
break;
}
}
if (sigma) {
cout << "TAK\n";
}
}
}
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 | #include <bits/stdc++.h> using namespace std; int color[100009]; vector<int> graph[100009]; bool visited[100009]; vector<int> ofcol[100009]; vector<int> f; int dfs(int node, int c) { f.push_back(node); visited[node] = true; int s = 0; if (color[node] == c) { s = 1; } for (auto x: graph[node]) { if ((color[x] == c or color[x] == 0) and !visited[x]) { s += dfs(x,c); } } return s; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; cin>>t; while(t--) { int n,m,k; cin>>n>>m>>k; for (int i = 1; i<=k; i++) { ofcol[i].clear(); } for (int i = 1; i<=n; i++) { cin>>color[i]; graph[i].clear(); ofcol[color[i]].push_back(i); visited[i] = false; } for (int i = 0; i<m; i++) { int a,b; cin>>a>>b; graph[a].push_back(b); graph[b].push_back(a); } unordered_set<int> left; for (int i = 1; i<=k; i++) { left.insert(i); } bool sigma = true; while (!left.empty()) { int g = -1; for (auto x: left){ int ile = 0; if (ofcol[x].size() != 0) { f.clear(); ile = dfs(ofcol[x][0],x); for (auto q: f) { visited[q] = false; } } if (ile == ofcol[x].size()) { g = x; left.erase(x); break; } } if (g != -1) { for (auto x: ofcol[g]) { color[x] = 0; } }else { cout << "NIE\n"; sigma = false; break; } } if (sigma) { cout << "TAK\n"; } } } |
English