# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
276066 | abyyskit | Race (IOI11_race) | C++14 | 3085 ms | 29200 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.
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for(int i = x; i < y; ++i)
#define pb push_back
int ANS;
int TIME;
int K;
struct node{
vector<int> e;
vector<long long> w;
int start = -1;
int end;
vector<int> l = vector<int>(19);
long long p = 0;
int h;
bool vis = false;
};
vector<node> g;
void dfs(int start, int height){
g[start].start = TIME;
g[start].h = height;
g[start].vis = true;
TIME++;
FOR(i, 0, g[start].e.size()){
int nex = g[start].e[i];
if (!g[nex].vis){
g[nex].l[0] = start;
g[nex].p = g[start].p + g[start].w[i];
dfs(nex, height + 1);
}
}
g[start].end = TIME;
TIME++;
}
void setup_LCA(){
g[0].l[0] = 0;
FOR(i, 0, g.size()){
FOR(j, 1, g[i].l.size()){
g[i].l[j] = g[g[i].l[j - 1]].l[j - 1];
}
}
}
int LCA(int a, int b){
int cur = a;
for (int i = g[a].l.size() - 1; i >= 0; --i){
int nex = g[cur].l[i];
if (!(g[nex].start <= g[b].start and g[b].start <= g[nex].end)){
cur = nex;
}
}
int tmp = 0;
while (!(g[cur].start <= g[b].start and g[b].start <= g[cur].end)){
cur = g[cur].l[0];
tmp++;
}
// cout << "LCA(" << a << ", " << b << ") = " << cur << ", ";
return cur;
}
int solve(int a, int b){
int c = LCA(a, b);
int dis = g[a].h + g[b].h - 2*g[c].h;
long long odis = g[a].p + g[b].p - 2*g[c].p;
// cout << "dis(" << a << ", " << b << ") = " << dis << ", ";
// cout << "odis(" << a << ", " << b << ") = " << odis << ", ";
// cout << "\n";
if (odis == K){
return dis;
}
else{
return g.size() + 1;
}
}
int best_path(int N, int k, int H[][2], int L[]){
ANS = N + 1;
K = k;
TIME = 0;
g.resize(N);
FOR(i, 0, N-1){
g[H[i][0]].e.pb(H[i][1]);
g[H[i][0]].w.pb(L[i]);
g[H[i][1]].e.pb(H[i][2]);
g[H[i][1]].w.pb(L[i]);
}
dfs(0, 0);
setup_LCA();
FOR(i, 0, N){
FOR(j, i + 1, N){
ANS = min(ANS, solve(i, j));
}
}
if (ANS == N + 1){
ANS = -1;
}
return ANS;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |