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"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef int ll;
typedef double db;
typedef pair<ll,ll> pl;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define po pop_back
#define pb push_back
#define mk make_pair
#define mt make_tuple
#define lw lower_bound
#define up upper_bound
#define ff first
#define ss second
#define BOOST ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0)
#define MOD 1000000007
#define MAX 1e18
#define MIN -1e18
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define per(i,a,b) for(ll i=b;i>=a;i--)
#define con continue
#define freopen freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define PI 3.14159265358979323846264338327950288419716939937510582097494459230781640628
#define read(x) scanf("%lld",&x);
#define print(x) printf("%lld ",x);
#define endl '\n';
typedef tree<ll , null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multi_set;
typedef tree<ll , null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
// template< typename T>
// using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll n,m,ans,mid,mn,mx,cnt,T,sum,h1,h2,e[1234567],b,c[1234567],d[1<<20],k,i,j,l,r,h,a[1234567],w,x,y,z,t,xx,yy;
bool used[1234567];
pl p[1234567];
vector<pl> v[123456],vec,vv1,vv2,vpl,vp;
string s1,s;
ll sz[1234567];
// ll tr[5234567],par[1234567],maxx,arr[1234567];
ll dx[4]={-1,1,0,0},dy[4]={0,0,-1,1},c1[123][123];
map<ll,ll> mp;
ll son[1234567],arr[1234567],node,dist[1234567];
void init(int node , int par){
son[node] = 1;
for(auto &u : v[node]){
int x = u.ff;
if(used[x] || x == par) continue;
init(x,node);
son[node] += son[x];
}
}
int findcen(int node, int sz){
for(auto &p : v[node]){
int x = p.ff;
if(used[x] || son[x] > son[node]) continue;
if(son[x]*2 >= sz) return findcen(x , sz);
}
return node;
}
void dfs(int node, int par=-1, int lvl=0, int cnt=0){
for(auto &p : v[node]){
int x,c;
x = p.ff;
c = p.ss;
if(x == par || used[x]) continue;
dfs(x , node , lvl + c , cnt+1);
}
if(0<lvl && lvl<=k){
vpl.pb({lvl, cnt});
}
}
int solve(int id){
init(id,-1);
id = findcen(id, son[id]);
used[id]=true;
int ans=1e9;
for(auto &p : v[id]){
int x,c;
x = p.ff;
c = p.ss;
if(used[x]) continue;
dfs(x,node,c,1);
for(auto &u : vpl){
int lvl, cnt;
lvl = u.ff;
cnt = u.ss;
ans = min(ans, cnt + dist[k - lvl]);
}
for(auto &u : vpl){
int lvl, cnt;
lvl = u.ff;
cnt = u.ss;
dist[lvl] = min(dist[lvl], cnt);
vp.pb(u);
}
vpl.clear();
}
for(auto &u : vp){
int lvl = u.ff;
dist[lvl] = 1e9;
}
vp.clear();
for(auto &p : v[id]){
int x = p.ff;
if(used[x]) continue;
ans=min(solve(x) , ans);
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]){
ans = 1e9;
n = N;
k = K;
for(int i=0;i<N-1;i++){
v[H[i][0]].pb({H[i][1],L[i]});
v[H[i][1]].pb({H[i][0],L[i]});
}
for(int i=0;i<=k+1;i++)dist[i]=1e9;
ans = solve(0);
if(ans == 1e9)return -1;
else 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... |