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 <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define F first
#define S second
#define PB push_back
#define INF 1000000000
int n,k,vs[200005],w[200005],la[200005],ans=INF;
vector<P>g[200005];
map<int,int>m[200005];
void merge(int x,int y){
if(m[vs[x]].size()<m[vs[y]].size())swap(x,y);
int vx=vs[x],vy=vs[y];
for(map<int,int>::iterator i=m[vy].begin();i!=m[vy].end();i++){
if(m[vx].find(k-w[vx]-w[vy]-i->F)!=m[vx].end())ans=min(ans,m[vx][k-w[vx]-w[vy]-i->F]+i->S+la[vx]+la[vy]);
}
for(map<int,int>::iterator i=m[vy].begin();i!=m[vy].end();i++){
int c=i->F-w[vx]+w[vy];
if(m[vx].find(c)!=m[vx].end())m[vx][c]=min(m[vx][c],i->S-la[vx]+la[vy]);
else m[vx][c]=i->S-la[vx]+la[vy];
}
vs[y]=vx;
}
void dfs(int v,int p){
for(int i=0;i<g[v].size();i++){
int u=g[v][i].F,l=g[v][i].S;
if(u==p)continue;
dfs(u,v);
w[vs[u]]+=l;
la[vs[u]]++;
merge(v,u);
}
}
int best_path(int N,int K,int H[][2],int L[]){
n=N;
k=K;
for(int i=0;i<n-1;i++){
int a=H[i][0],b=H[i][1],c=L[i];
g[a].PB(P(b,c));
g[b].PB(P(a,c));
}
for(int i=0;i<n;i++)vs[i]=i,m[i][0]=0;
dfs(0,-1);
if(ans==INF)return -1;
else return ans;
}
Compilation message (stderr)
race.cpp: In function 'void dfs(int, int)':
race.cpp:25:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[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... |