# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1126766 | KasymK | Cop and Robber (BOI14_coprobber) | C++20 | 0 ms | 0 KiB |
#include "bits/stdc++.h"
#include "coprobber.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 505;
const int B = 10;
const int INF = 1e9;
vector<int> adj[N];
int d[N], now, dis[N][N], sp[N][B];
void dfs(int x=0, int pr=-1){
tr(it, adj[x])
if(*it!=pr){
d[*it]=d[x]+1;
sp[x][0]=pr;
dfs(*it, x);
}
}
int get(int x, int k){
for(int i = 0; i < B; ++i)
if(k>>i&1)
x=sp[x][i];
return x;
}
int lca(int a, int b){
if(d[a]<d[b])
swap(a, b);
int ad = d[a]-d[b];
a = get(a, ad);
if(a==b)
return a;
for(int i = B; i >= 0; --i)
if(sp[a][i]!=sp[b][i])
a=sp[a][i], b=sp[b][i];
return sp[a][0];
}
int calc(int i, int j){
return d[i]+d[j]-2*d[lca(i, j)];
}
int start(int n, bool v[N][N]){
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(v[i][j])
adj[i].pb(j), adj[j].pb(i);
dfs();
for(int i = 0; i < n; ++i)
for(int j = 1; j < B; ++j)
sp[i][j] = sp[sp[i][j-1]][j-1];
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
dis[i][j] = calc(i, j);
now = 0;
return now;
}
int nextMove(int r){
int mn = INF;
tr(it, adj[now])
if(umin(mn, dis[r][*it]))
now=*it;
return now;
}