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
{
long long int a,b,c;
void read()
{
cin>>a>>b>>c;
}
};
long long int n,k,used[200005],dp[200005];
cell a[200005];
vector<cell>v[200005];
map<long long int,long long int>m,ne;
long long int otg;
void make_dp(long long int beg,int par)
{
long long int w;
dp[beg]=1;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w]&&w!=par)
{
make_dp(w,beg);
dp[beg]+=dp[w];
}
}
}
void cl(long long int beg,long long int par)
{
long long int w;
used[beg]=0;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(w!=par&&used[w]!=2)cl(w,beg);
}
}
void cdp(int beg,int par)
{
int w;
dp[beg]=0;
for(int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w]&&w!=par)cdp(w,beg);
}
}
void dfs(long long int beg,long long int st,long long int lead,long long int br)
{
if(st>k)return;
used[beg]=1;
cell w;
if(!ne[st])
{
ne[st]=br;
}
else ne[st]=min(ne[st],br);
for(long long 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++)
{
long long int h=j->first;
if(k==h)otg=min(otg,j->second);
if(m[k-h])
{
otg=min(otg,m[k-h]+j->second);
}
}
for(auto j=ne.begin();j!=ne.end();j++)
{
long long int h=j->first;
if(m[h])m[h]=min(m[h],ne[h]);
else m[h]=ne[h];
}
ne.clear();
}
}
}
void find_centroid(long long int beg,long long int par,long long int br)
{
long long int w,l=0;
used[beg]=1;
for(long long 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);
dfs(beg,0,beg,0);
m.clear();
cl(beg,beg);
cdp(beg,beg);
used[beg]=2;
for(long long int i=0;i<v[beg].size();i++)
{
w=v[beg][i].b;
if(!used[w])
{
make_dp(w,beg);
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(long long 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(long long 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]);
}
//cout<<endl;
otg=1000000000;
cl(0,0);
find_centroid(0,0,dp[0]);
if(otg==1000000000)
{
//cout<<-1<<endl;
return -1;
}
return otg;
//cout<<otg<<endl;
}
/*int main()
{
speed();
read();
return 0;
}
*/
Compilation message (stderr)
race.cpp: In function 'void make_dp(long long int, int)':
race.cpp:27:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void cl(long long int, long long int)':
race.cpp:41:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void cdp(int, int)':
race.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
race.cpp:67:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp: In function 'void find_centroid(long long int, long long int, long long int)':
race.cpp:99:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(long long int i=0;i<v[beg].size();i++)
| ~^~~~~~~~~~~~~~
race.cpp:116:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<cell>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for(long long 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... |