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 =200'010;
long long K=0;
vector<pair<int, long long>> g[N];
vector<bool> R(N);
int dp[N];
int dfs(int at, int p) {
dp[at]=1;
for(auto to:g[at]) {
if(to.first!=p and !R[to.first]) {
dp[at]+=dfs(to.first,at);
}
}
return dp[at];
}
int get_centroid(int at, int sz, int p) {
for(auto to:g[at]) {
if(to.first==p or R[to.first])continue;
if(dp[to.first]*2>sz)return get_centroid(to.first,sz,at);
}
return at;
}
long long res=1e18;
map<long long, int> mp;
map<long long, int> mp2;
void dfs1(int at, int p, long long w, int d=1) {
if(mp2.find(w)==mp2.end() or mp2[w]>d) {
mp2[w]=d;
if(mp.find(K-w)!=mp.end())
res=min(res,(long long)mp2[w]+mp[K-w]);
}
for(auto to:g[at]) {
if(R[to.first])continue;
if(to.first==p)continue;
dfs1(to.first,at,w+to.second,d+1);
}
}
void get(int at) {
mp.clear();
mp2.clear();
mp[0]=0;
for(auto to:g[at]) {
if(R[to.first])continue;
dfs1(to.first,at,to.second);
for(auto i:mp2) {
if(mp.find(i.first)==mp.end())mp[i.first]=i.second;
else mp[i.first]=min(mp[i.first],i.second);
}
mp2.clear();
}
}
void centroid(int n=0) {
int C=get_centroid(n,dfs(n,-1),-1);
get(C);
R[C]=1;
for(auto to:g[C]) {
if(!R[to.first])centroid(to.first);
}
}
int best_path(int n, int k, int h[][2], int l[])
{
K=k;
for(int i = 0;i<n-1;i++) {
g[h[i][0]].push_back({h[i][1],l[i]});
g[h[i][1]].push_back({h[i][0],l[i]});
}
centroid();
if(res==1e18)return -1;
return res;
}
# | 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... |