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 <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n,k;
int answer = 999999999;
vector<pair<int,int> > edge[200002];
queue<pair<int,int> > q,qt;
int root[1000002],small[1000002];
bool check[200002];
queue<int> qv;
int downcnt[200002];
int max_subtree[200002];
int j,nextv,tmp;
pair<int,int> top;
int make_qv(int x){
int i;
check[x] = true;
qv.push(x);
downcnt[x] = max_subtree[x] = 0;
for(i=0; i<edge[x].size(); i++){
nextv = edge[x][i].first;
if(!check[nextv]){
tmp = make_qv(nextv);
downcnt[x] += tmp;
if(max_subtree[x] < tmp) max_subtree[x] = tmp;
}
}
check[x] = false;
return (downcnt[x]+1);
}
int find_middle(int x){
int i,below_x;
int big = 0,v;
below_x = make_qv(x);
while(!qv.empty()){
v = qv.front();
tmp = min(below_x - max_subtree[v],max_subtree[v]+1);
if(big < tmp){
big = tmp;
x = v;
}
qv.pop();
}
return x;
}
void dfs(int v,int sum,int cnt){
int i;
if(sum > k) return;
check[v] = true;
q.push(make_pair(sum,cnt));
qt.push(make_pair(sum,cnt));
for(i=0; i<edge[v].size(); i++){
nextv = edge[v][i].first;
if(check[nextv]) continue;
dfs(nextv,sum+edge[v][i].second,cnt+1);
}
check[v] = false;
}
// T(n) = 2T(n/2) + O(n);
void race(int x){
int i;
int v = find_middle(x);
check[v] = true;
root[0] = v;
small[0] = 0;
for(i=0; i<edge[v].size(); i++){
nextv = edge[v][i].first;
if(check[nextv]) continue;
dfs(nextv,edge[v][i].second,1);
while(!q.empty()){
top = q.front();
if(root[k-top.first] == v) answer = min(answer,small[k-top.first] + top.second);
q.pop();
}
while(!qt.empty()){
top = qt.front();
qt.pop();
if(root[top.first] != v){
root[top.first] = v;
small[top.first] = top.second;
}else if(small[top.first] > top.second) small[top.first] = top.second;
}
}
for(i=0; i<edge[v].size(); i++){
nextv = edge[v][i].first;
if(!check[nextv]) race(nextv);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N; k = K;
for(j=0; j<n-1; j++){
edge[H[j][0]].push_back(make_pair(H[j][1],L[j]));
edge[H[j][1]].push_back(make_pair(H[j][0],L[j]));
}
for(j=0; j<=k; j++) root[j] = -1;
race(0);
if(answer == 999999999) answer = -1;
return answer;
}
Compilation message (stderr)
race.cpp: In function 'int make_qv(int)':
race.cpp:26:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[x].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'int find_middle(int)':
race.cpp:39:9: warning: unused variable 'i' [-Wunused-variable]
int i,below_x;
^
race.cpp: In function 'void dfs(int, int, int)':
race.cpp:64:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'void race(int)':
race.cpp:82:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
race.cpp:103:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<edge[v].size(); i++){
~^~~~~~~~~~~~~~~
# | 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... |