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;
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 (stderr)
race.cpp: In function 'int fnd_cntr(int, int, int)':
race.cpp:18:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs1(int, int, int, int)':
race.cpp:39:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs2(int, int, int, int)':
race.cpp:52:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
race.cpp: In function 'void build(int, int)':
race.cpp:66:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[cntr].size(); i++){
~~^~~~~~~~~~~~~~~~
race.cpp:76:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[cntr].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... |