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 <map>
#include <iostream>
using namespace std;
const int nmax=200005;
map<long long,int> m[nmax];
vector< pair<int,int> > v[nmax];
long long k;
long long L[nmax];
int rr[nmax],l[nmax],r[nmax],lev[nmax],w[nmax],p[nmax];
int mn,nr,paths,n,i;
void ver(int path,long long A,long long LCA,int lca,int x)
{
//A+X-2*LCA=K
//X=K-A+2*LCA
if(m[path][k-A+2*LCA]&&m[path][k-A+2*LCA]+x-2*lca<mn)
mn=m[path][k-A+2*LCA]+x-2*lca;
}
void baga(int path,long long A,int x)
{
if((!m[path][A])||m[path][A]>x)
m[path][A]=x;
}
void dfs(int x)
{
int son=n+1,nod=0;
rr[++nr]=x;l[x]=nr;w[x]=1;
for(int i=0;i<v[x].size();i++)
if(!w[v[x][i].first])
{
nod=v[x][i].first;
lev[nod]=lev[x]+1;L[nod]=1LL*L[x]+v[x][i].second;
dfs(nod);
w[x]+=w[nod];
if(w[nod]>w[son]) son=nod;
}
r[x]=nr;
p[x]=p[son];
if(!son) p[x]=++paths;
for(int i=0;i<v[x].size();i++)
if(w[v[x][i].first]<=w[x]&&son!=v[x][i].first)
{
nod=v[x][i].first;
for(int j=l[nod];j<=r[nod];j++)
{
ver(p[x],L[rr[j]],L[x],lev[x],lev[rr[j]]);
}
for(int j=l[nod];j<=r[nod];j++)
{
baga(p[x],L[rr[j]],lev[rr[j]]);
}
}
ver(p[x],L[x],L[x],lev[x],lev[x]);
baga(p[x],L[x],l[x]);
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N,k=K;
for(i=0;i<n-1;i++)
{
v[H[i][0]].push_back({H[i][1],L[i]});
v[H[i][1]].push_back({H[i][0],L[i]});
}
lev[0]=1;mn=n+1;
dfs(0);
if(mn==n+1) mn=-1;
return mn;
}
Compilation message (stderr)
race.cpp: In function 'void dfs(int)':
race.cpp:29:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
race.cpp:41:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].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... |