#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
const int MAXN = 100005;
const int LOGN = 20;
int up[MAXN][LOGN];
int tin[MAXN], tout[MAXN], depth[MAXN];
int timer = 0;
void dfs_lca(int u, int p, int d, const vector<vector<int>> &adj)
{
tin[u] = ++timer;
depth[u] = d;
up[u][0] = p;
for (int i = 1; i < LOGN; i++)
{
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (int i = 0; i < (int)adj[u].size(); i++)
{
int v = adj[u][i];
if (v != p)
{
dfs_lca(v, u, d + 1, adj);
}
}
tout[u] = timer;
}
bool is_ancestor(int u, int v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int get_lca(int u, int v)
{
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = LOGN - 1; i >= 0; i--)
{
if (!is_ancestor(up[u][i], v))
{
u = up[u][i];
}
}
return up[u][0];
}
bool compareByTin(int a, int b)
{
return tin[a] < tin[b];
}
vector<int> last_visited(MAXN, -1);
vector<int> color_mark(MAXN, -1);
bool hasCycleDFS(int u, const vector<vector<int>> &adj, vector<int> &state)
{
state[u] = 1;
for (int i = 0; i < (int)adj[u].size(); i++)
{
int v = adj[u][i];
if (state[v] == 1)
return true;
if (state[v] == 0 && hasCycleDFS(v, adj, state))
return true;
}
state[u] = 2;
return false;
}
bool checkCycles(int k, const vector<vector<int>> &adj)
{
vector<int> state(k, 0);
for (int i = 0; i < k; i++)
{
if (state[i] == 0)
{
if (hasCycleDFS(i, adj, state))
return true;
}
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
{
int n, m, k;
cin >> n >> m >> k;
timer = 0;
fill(last_visited.begin(), last_visited.begin() + n + 1, -1);
fill(color_mark.begin(), color_mark.begin() + k + 1, -1);
for (int i = 0; i <= n; i++)
{
tin[i] = tout[i] = 0;
for (int j = 0; j < LOGN; j++)
up[i][j] = 0;
}
vector<int> color(n);
vector<vector<int>> neighbors(n);
vector<vector<int>> nodesWithColor(k);
for (int i = 0; i < n; i++)
{
cin >> color[i];
color[i]--;
nodesWithColor[color[i]].push_back(i);
}
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
neighbors[a - 1].push_back(b - 1);
neighbors[b - 1].push_back(a - 1);
}
if (m != n - 1)
{
cout << "TAK" << endl;
continue;
}
vector<vector<int>> colorDependencies(k);
dfs_lca(0, 0, 0, neighbors);
if (timer != n)
{
cout << "TAK" << endl;
continue;
}
// queries
for (int i = 0; i < k; i++)
{
if (nodesWithColor[i].size() == 0)
continue;
// get root lca
int root_lca = nodesWithColor[i][0];
for (int node : nodesWithColor[i])
{
root_lca = get_lca(root_lca, node);
}
for (int u : nodesWithColor[i])
{
int curr = u;
// go to root to find all colors
while (true)
{
if (last_visited[curr] == i)
break;
if (color[curr] != i)
{
if (color_mark[color[curr]] != i)
{
colorDependencies[i].push_back(color[curr]);
color_mark[color[curr]] = i;
}
}
last_visited[curr] = i;
if (curr == root_lca)
break;
curr = up[curr][0];
}
}
}
// check the dependency graph for cycles
bool hasCycles = checkCycles(k, colorDependencies);
if (hasCycles)
{
cout << "NIE" << endl;
}
else
{
cout << "TAK" << endl;
}
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <cassert> using namespace std; const int MAXN = 100005; const int LOGN = 20; int up[MAXN][LOGN]; int tin[MAXN], tout[MAXN], depth[MAXN]; int timer = 0; void dfs_lca(int u, int p, int d, const vector<vector<int>> &adj) { tin[u] = ++timer; depth[u] = d; up[u][0] = p; for (int i = 1; i < LOGN; i++) { up[u][i] = up[up[u][i - 1]][i - 1]; } for (int i = 0; i < (int)adj[u].size(); i++) { int v = adj[u][i]; if (v != p) { dfs_lca(v, u, d + 1, adj); } } tout[u] = timer; } bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; } int get_lca(int u, int v) { if (is_ancestor(u, v)) return u; if (is_ancestor(v, u)) return v; for (int i = LOGN - 1; i >= 0; i--) { if (!is_ancestor(up[u][i], v)) { u = up[u][i]; } } return up[u][0]; } bool compareByTin(int a, int b) { return tin[a] < tin[b]; } vector<int> last_visited(MAXN, -1); vector<int> color_mark(MAXN, -1); bool hasCycleDFS(int u, const vector<vector<int>> &adj, vector<int> &state) { state[u] = 1; for (int i = 0; i < (int)adj[u].size(); i++) { int v = adj[u][i]; if (state[v] == 1) return true; if (state[v] == 0 && hasCycleDFS(v, adj, state)) return true; } state[u] = 2; return false; } bool checkCycles(int k, const vector<vector<int>> &adj) { vector<int> state(k, 0); for (int i = 0; i < k; i++) { if (state[i] == 0) { if (hasCycleDFS(i, adj, state)) return true; } } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, m, k; cin >> n >> m >> k; timer = 0; fill(last_visited.begin(), last_visited.begin() + n + 1, -1); fill(color_mark.begin(), color_mark.begin() + k + 1, -1); for (int i = 0; i <= n; i++) { tin[i] = tout[i] = 0; for (int j = 0; j < LOGN; j++) up[i][j] = 0; } vector<int> color(n); vector<vector<int>> neighbors(n); vector<vector<int>> nodesWithColor(k); for (int i = 0; i < n; i++) { cin >> color[i]; color[i]--; nodesWithColor[color[i]].push_back(i); } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; neighbors[a - 1].push_back(b - 1); neighbors[b - 1].push_back(a - 1); } if (m != n - 1) { cout << "TAK" << endl; continue; } vector<vector<int>> colorDependencies(k); dfs_lca(0, 0, 0, neighbors); if (timer != n) { cout << "TAK" << endl; continue; } // queries for (int i = 0; i < k; i++) { if (nodesWithColor[i].size() == 0) continue; // get root lca int root_lca = nodesWithColor[i][0]; for (int node : nodesWithColor[i]) { root_lca = get_lca(root_lca, node); } for (int u : nodesWithColor[i]) { int curr = u; // go to root to find all colors while (true) { if (last_visited[curr] == i) break; if (color[curr] != i) { if (color_mark[color[curr]] != i) { colorDependencies[i].push_back(color[curr]); color_mark[color[curr]] = i; } } last_visited[curr] = i; if (curr == root_lca) break; curr = up[curr][0]; } } } // check the dependency graph for cycles bool hasCycles = checkCycles(k, colorDependencies); if (hasCycles) { cout << "NIE" << endl; } else { cout << "TAK" << endl; } } return 0; } |
English