# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
112966 | imaxblue | Highway Tolls (IOI18_highway) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
int n, m, a, b, sz[90005], ds[90005];
ll dis;
vector<pii> v[90005];
bool u[90005], typ;
vector<int> on;
vector<int> out;
bool split = 0;
void dfs0(int N, int P){
sz[N] = 1;
for(auto i:v[N]){
if (i.x == P || u[i.y]) continue;
dfs0(i.x, N);
sz[N]+=sz[i.x];
}
//cout <<"*" << N << ' ' << sz[N] << endl;
}
void dfs2(int N, int P){
for(auto i:v[N]){
if (u[i.y] || i.x == P) continue;
on[i.y] = typ;
dfs2(i.x, N);
}
}
void dfs3(int N, int P, int D){
ds[N] = D;
for(auto i:v[N]){
if (u[i.y] || i.x == P) continue;
dfs3(i.x, N, D+1);
}
}
void dfs(int N){
//cout << "?";
dfs0(N, -1);
bool done = 0;
int R = N;
int S = sz[N], P = -1, last;
if (S == 1){
out.pb(N);
return;
}
while(!done){
done = 1;
for(auto i:v[N]){
if (u[i.y] || i.x == P) continue;
if (sz[i.x]*2 >= S){
P = N;
N = i.x;
last = i.y;
done = 0;
break;
}
}
}
bool spec=0;
if (P == -1){
spec= 1;
int hs = 0;
for(auto i:v[N]){
if (u[i.y]) continue;
if (sz[i.x] > hs){
hs = sz[i.x];
P = i.x;
last = i.y;
}
}
}
//cout << "?" << last << endl;
on[last] = 1;
typ = 0;
dfs2(P, N);
typ = 1;
dfs2(N, P);
bool res1 = (ask(on) - dis > 0);
typ = 1;
dfs2(P, N);
typ = 0;
dfs2(N, P);
bool res2 = (ask(on) - dis > 0);
//cout << "*" << N << ' ' << P << ' ' << last << ' ' << res1 << ' ' << res2 << ' ' << split<< endl;
typ = 0;
dfs2(P, N);
typ = 0;
dfs2(N, P);
on[last] = 0;
if (res1 == 0 && res2 == 0 && split == 0){
out.pb(N); out.pb(P);
return;
} else if (res1 == 0 && res2 == 0){
out.pb(R);
return;
}
u[last] = 1;
if (!split){
if (res1 && res2){
split = 1;
dfs3(N, -1, 0);
}
if (res1){
dfs(N);
}
if (res2){
dfs(P);
}
}
else if (split){
if (ds[N] > ds[P]){
if (res1 ){
dfs(N);
} else {
dfs(P);
}
} else {
if (res2){
dfs(P);
} else {
dfs(N);
}
}
}
}
void find_pair(int N,vector<int> U, vector<int> V, int A, int B){
n = N; m = U.size(); a = A; b = B;
on = vector<int>(m, 0);
fox(l, m){
v[U[l]].pb(mp(V[l], l));
v[V[l]].pb(mp(U[l], l));
}
dis = ask(vector<int>(m, 0));
dfs(0);
//cout << out.size() << endl;
answer(out[0], out[1]);
}