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>
#include "race.h"
using namespace std;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
struct cell
{
int a,b,c;
void read()
{
cin>>a>>b>>c;
}
};
int n,k,used[200005],dp[200005];
cell a[200005];
vector<cell>v[200005];
map<int,int>m,ne;
int otg;
void make_dp(int beg)
{
int w;
used[beg]=1;
dp[beg]=1;
for(int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w])
{
make_dp(w);
dp[beg]+=dp[w];
}
}
}
void cl(int beg,int par)
{
int w;
used[beg]=0;
for(int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(w!=par&&used[w]!=2)cl(w,beg);
}
}
void dfs(int beg,int st,int lead,int br)
{
used[beg]=1;
cell w;
if(!ne[st])
{
ne[st]=br;
}
else ne[st]=min(ne[st],br);
for(int i=0;i<v[beg].size();i++)
{
w=v[beg][i];
if(!used[w.b])
{
dfs(w.b,st+w.c,lead,br+1);
}
if(beg==lead)
{
for(auto j=ne.begin();j!=ne.end();j++)
{
int h=j->first;
if(m[k-h])otg=min(otg,m[k-h]+j->second);
if(m[h])m[h]=min(m[h],ne[h]);
else m[h]=ne[h];
}
ne.clear();
}
}
}
void find_centroid(int beg,int par,int br)
{
int w,l=0;
used[beg]=1;
for(int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w]&&dp[w]>=(br+1)/2)
{
find_centroid(w,beg,br);
l=1;
}
}
if(!l)
{
cl(beg,beg);
m[0]=0;
dfs(beg,0,beg,0);
m.clear();
cl(beg,beg);
used[beg]=2;
for(int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w])
{
make_dp(w);
cl(w,w);
find_centroid(w,w,dp[w]);
}
}
}
}
int best_path(int N,int K,int H[][2],int L[])
//void read()
{
//cin>>n>>k;
n=N;
k=K;
for(int i=1;i<n;i++)
{
//cin>>a[i].a>>a[i].b;
a[i].a=H[i][0];
a[i].b=H[i][1];
}
for(int i=1;i<n;i++)
{
//cin>>a[i].c;
a[i].c=L[i];
v[a[i].a].push_back(a[i]);
swap(a[i].a,a[i].b);
v[a[i].a].push_back(a[i]);
}
otg=1000000000;
dfs(0,0,0,0);
make_dp(0);
cl(0,0);
find_centroid(0,0,dp[0]);
cout<<otg<<endl;
if(otg==1000000000)
{
//cout<<-1<<endl;
return -1;
}
return otg;
}
/*int main()
{
speed();
read();
return 0;
}
*/
Compilation message (stderr)
race.cpp: In function 'void make_dp(int)':
race.cpp:28:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void cl(int, int)':
race.cpp:42:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:57:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void find_centroid(int, int, int)':
race.cpp:81:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp:98:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int i=0;i<v[beg].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... |