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>
#define xx first
#define yy second
using namespace std;
long long int typedef li;
vector<vector<pair<int,int>>> g(200005);
int ch[200005],ans=1e9;
bitset<200005> vis;
li K;
void dfs(int p, int q)
{
ch[p]=1;
for (auto [it,w] : g[p]) if (it!=q && !vis[it]) dfs(it,p),ch[p]+=ch[it];
}
int centroid(int p, int q, int s)
{
int c=p;
for (auto [it,w] : g[p]) if (it!=q && !vis[it])
{
int tmp=centroid(it,p,s);
if (tmp!=-1) return tmp;
if (2*ch[it]>s) c=-1;
}
if (2*ch[p]<s) c=-1;
return c;
}
unordered_map<li,int> dub;
void add(int p, int q, li s, int d)
{
auto it=dub.find(s);
if (it==dub.end()) dub[s]=d;
else it->yy=min(it->yy,d);
for (auto [it,w] : g[p]) if (it!=q && !vis[it]) add(it,p,s+w,d+1);
}
void iter(int p, int q, li s, int d)
{
auto it=dub.find(K-s);
if (it!=dub.end()) ans=min(ans,d+it->yy);
for (auto [it,w] : g[p]) if (it!=q && !vis[it]) iter(it,p,s+w,d+1);
}
void decomp(int p)
{
dfs(p,-1);
int c=centroid(p,-1,ch[p]);
//cout<<","<<c<<endl;
dub.clear(); dub[0]=0;
for (auto [it,w] : g[c]) if (!vis[it]) iter(it,c,w,1),add(it,c,w,1);
vis[c]=1;
for (auto [it,w] : g[c]) if (!vis[it]) decomp(it);
}
int best_path(int n, int k, int H[][2], int L[])
{
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]});
K=k,decomp(0); return (ans==1e9?-1:ans);
}
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
7 3
0 1 1
0 4 1
4 5 1
5 6 1
1 2 3
1 3 2
7 8
0 1 1
0 4 1
4 5 1
5 6 1
1 2 3
1 3 2
*/
# | 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... |