This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 500
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll mod=1000000007;//998244353;
const ll inf=1e18*4;
const ld pai=acos(-1);
#include "race.h"
int n,k,ans=1e9;
vpi v[200009];
int from[1000009];
int sz[200009],done[200009],P[20009];
vpi all,ret;
void dfssz(int node,int p){
P[node]=p;
sz[node]=1;
for(auto u:v[node]){
if(u.fi==p)C;
if(done[u.fi])C;
dfssz(u.fi,node);
sz[node]+=sz[u.fi];
}
}
void dfs(int node,int p,int t,int num){
if(t>k)return ;
ans=min(ans,from[k-t]+num);
ret.pb({t,num});
all.pb({t,num});
for(auto u:v[node]){
if(done[u.fi] || u.fi==p)C;
dfs(u.fi,node,t+u.se,num+1);
}
}
void solve(int node){
ret.clear();
all.clear();
for(auto u: v[node]){
if(done[u.fi])C;
dfs(u.fi,u.fi,u.se,1);
for(auto x:ret){
from[x.fi]=min(from[x.fi],x.se);
}
ret.clear();
}
}
void erase(){
for(auto u:all)from[u.fi]=1e9;
all.clear();
}
void create(int start){
dfssz(start,start);
queue<int>q;q.push(start);
int best=1e9,who=-1;
while(q.size()){
int node=q.front();q.pop();
int s=sz[start]-sz[node];
for(auto u :v[node]){
if(done[u.fi] || P[node]==u.fi)C;
q.push(u.fi);
s=max(s,sz[u.fi]);
}
if(s<best)best=s,who=node;
}
done[who]=1;
solve(who);
erase();
for(auto u: v[who]){
if(done[u.fi])C;
create(u.fi);
}
}
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,b,c;
a=H[i][0],b=H[i][1],c=L[i];
v[a].pb({b,c});
v[b].pb({a,c});
}
for(int i=1;i<=1e3;i++)from[i]=1e9;
create(0);
if(ans==1e9)return -1;
return ans;
}
# | 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... |