# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
63014 |
2018-07-31T09:50:27 Z |
zubec |
Race (IOI11_race) |
C++14 |
|
0 ms |
0 KB |
#include "grader.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
vector <pair<int, int> > g[200100];
int needLen, n, sz[N], pr[N];
bool used[N];
int ans = 1e9;
map<int, int> mp1;
int fnd_cntr(int v, int p, int szcmp){
int pos = -1;
bool bb = 0;
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i].first;
if (to == p || used[to])
continue;
if (sz[to]-1 >= szcmp/2){
bb = 1;
pos = to;
break;
}
}
if (bb == 0)
return v;
return fnd_cntr(pos, v, szcmp);
}
void dfs1(int v, int p, int len, int pth){
sz[v] = 1;
pr[v] = p;
if (mp1.find(needLen-len)!=mp1.end()){
ans = min(ans, mp1[needLen-len]+pth);
}
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i].first, curlen = g[v][i].second;
if (to == p || used[to])
continue;
dfs1(to, v, len+curlen, pth+1);
sz[v] += sz[to];
}
}
void dfs2(int v, int p, int len, int pth){
if (mp1.find(len) != mp1.end())
mp1[len] = min(mp1[len], pth); else
mp1[len] = pth;
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i].first, curlen = g[v][i].second;
if (to == p || used[to])
continue;
dfs2(to, v, len+curlen, pth+1);
}
}
void build(int v, int p){
if (used[v])
return;
int cntr = fnd_cntr(v, pr[v], sz[v]);
used[cntr] = 1;
mp1[0] = 0;
for (int i = 0; i < g[cntr].size(); i++){
int to = g[cntr][i].first, len = g[cntr][i].second;
if (used[to])
continue;
dfs1(to, 0, len, 1);
dfs2(to, 0, len, 1);
}
if (mp1.find(needLen) != mp1.end())
ans = min(ans, mp1[needLen]);
mp1.clear();
for (int i = 0; i < g[cntr].size(); i++){
int to = g[cntr][i].first;
if (!used[to])
build(to, cntr);
}
}
int best_path(int N, int K, int H[][2], int L[]){
needLen = K;
n = N;
for (int i = 1; i < n; i++){
int u = H[i-1][0], v = H[i-1][1], len = L[i-1];
g[u].push_back({v, len});
g[v].push_back({u, len});
}
dfs1(1, 0, 0, 0);
mp1.clear();
build(1, 0);
if (ans == 1e9)
ans = -1;
return ans;
}
Compilation message
race.cpp:1:10: fatal error: grader.h: No such file or directory
#include "grader.h"
^~~~~~~~~~
compilation terminated.