This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//apig's property
//Happiness can be found, even in the darkest of times, if one only remembers to turn on the light
//El Pueblo Unido Jamas Sera Vencido
//The saddest thing about betrayal? is that it never comes from your enemies
//Do or do not... there is no try
//Billions of bilious blue blistering barnacles in a thundering typhoon!
#include<bits/stdc++.h>
#include "race.h"
#define fast ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
#define F first
#define S second
#define pb push_back
#define vll vector< ll >
#define vi vector< int >
#define pll pair< ll , ll >
#define pi pair< int , int >
#define all(s) s.begin() , s.end()
#define sz(s) s.size()
#define str string
#define md ((s + e) / 2)
#define mid ((l + r) / 2)
#define msdp(dp) memset(dp , -1 , sizeof dp)
#define mscl(dp) memset(dp , 0 , sizeof dp)
#define C continue
#define R return
#define B break
#define lx node * 2
#define rx node * 2 + 1
using namespace std;
typedef long long ll;
ll q, dp[105][100005], vis[555555] , b[555555], k, l, m, n, o = 1e15 , p;
map < ll , ll > mp;
vector < pll > adj[555555];
const ll mod = 1e9+7;
str s;
void dfs1(ll v , ll par){
b[v] = 1 ;
for(auto u : adj[v]){
if(u.F == par || vis[u.F]) C ;
dfs1(u.F , v) ;
b[v] += b[u.F] ;
}
}
ll get(ll v , ll par , ll siz){
for(auto u : adj[v]){
if(u.F == par || vis[u.F] || b[u.F] <= siz / 2)C ;
R get(u.F , v , siz) ;
}
R v ;
}
void dfs2(ll v , ll par , ll dis , ll h){
if(dis > k)R ;
if(mp[k - dis]){
o = min(o , mp[k - dis] + h) ;
}
for(auto u : adj[v]){
if(u.F == par || vis[u.F])C ;
dfs2(u.F , v , dis + u.S , h + 1) ;
}
}
void dfs3(ll v , ll par , ll dis , ll h){
if(dis > k)R ;
if(mp[dis]){
mp[dis] = min(mp[dis] , h) ;
}
else {
mp[dis] = h ;
}
for(auto u : adj[v]){
if(u.F == par || vis[u.F])C ;
dfs2(u.F , v , dis + u.S , h + 1) ;
}
}
void dfs_calc(ll v){
dfs1(v , 0) ;
mp.clear() ;
ll op = get(v , 0 , b[v]) ;
vis[op]++ ;
for(auto u : adj[op]){
if(vis[u.F])C ;
dfs2(u.F , op , u.S , 1) ;
dfs3(u.F , op , u.S , 1) ;
}
for(auto u : adj[op]){
if(vis[u.F])C ;
dfs_calc(u.F) ;
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N ;
k = K ;
for(ll i = 0 ; i < n - 1 ; i++){
ll o1 = H[i][0] , o2 = H[i][1] ;
o1++ , o2++ ;
adj[o1].pb({o2 , L[i]}) , adj[o2].pb({o1 , L[i]}) ;
}
dfs_calc(1) ;
R (o < 1e15 ? o : -1) ;
}
# | 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... |