# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1269372 | rtri | Cop and Robber (BOI14_coprobber) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<bool>> adj;
vector<bool> vis;
bool has_cycle(int u, int p = -1, int gp = -1) {
// Special handle cycle of length 3
if (u == gp)
return false;
if (vis[u])
return true;
vis[u] = true;
for (int v = 0; v < n; v++)
if (adj[u][v] && v != p)
if (has_cycle(v, u, p))
return true;
return false;
}
vector<int> tin, tout, par;
int tick = 0;
void et(int u, int p = -1) {
tin[u] = tick++;
par[u] = p;
for (int v = 0; v < n; v++)
if (adj[u][v] && v != p)
et(v, u);
tout[u] = tick++;
}
bool subt(int u, int p) { return tin[p] <= tin[u] && tout[u] <= tout[p]; }
int cop = 0;
int start(int N, vector<vector<bool>> A) {
n = N;
adj = A;
vis.resize(n);
if (has_cycle(0))
return -1;
tin.resize(n);
tout.resize(n);
par.resize(n);
et(0);
return 0;
}
int nextMove(int R) {
for (int v = 0; v < n; v++)
if (adj[cop][v] && v != par[cop] && subt(R, v))
return v;
return par[cop];
}