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;
vector< pair<int,int> > edg[200005];
int k;
bool solved[200005];
int mxdeg, centroid;
vector< pair<long long, int> > v, v2;
int ans = 1e9;
void findcentroid(int x, int p) {
int deg = 0;
if(p!=-1) ++deg;
for(auto i:edg[x]) {
if(!solved[i.first] && i.first!=p) {
findcentroid(i.first, x);
++deg;
}
}
if(deg > mxdeg) {
mxdeg = deg;
centroid = x;
}
}
void solve(int x);
void findandsolve(int x) {
mxdeg = -1;
findcentroid(x, -1);
solve(centroid);
}
void dfs(int x, int p, long long len, int cnt) {
v.push_back({len, cnt});
for(auto i:edg[x]) {
if(solved[i.first]) continue;
if(i.first != p) {
dfs(i.first, x, len+i.second, cnt+1);
}
}
}
void solve(int x) {
v.clear(); v2.clear();
v2.push_back({0, 0});
solved[x] = true;
for(auto i:edg[x]) {
if(solved[i.first]) continue;
v.clear();
dfs(i.first, -1, i.second, 1);
sort(v.begin(), v.end());
for(auto i:v) {
int tmp = lower_bound(v2.begin(), v2.end(), make_pair(k-i.first, 0)) - v2.begin();
if(tmp < v2.size() && v2[tmp].first == k-i.first) {
ans = min(ans, v2[tmp].second + i.second);
}
}
for(auto i:v) {
v2.push_back(i);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
for(int i=0; i<N-1; i++) {
edg[H[i][0]].push_back({H[i][1], L[i]});
edg[H[i][1]].push_back({H[i][0], L[i]});
}
findandsolve(0);
if(ans==1e9) return -1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'void solve(int)':
race.cpp:56:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if(tmp < v2.size() && v2[tmp].first == k-i.first) {
| ~~~~^~~~~~~~~~~
# | 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... |