This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
STAY ORGANIZED
CHANGE YOUR APPROACH
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef long double ld ;
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define pb push_back
#define pi pair<int,int>
#define pll pair<ll,ll>
#define yes cout<<"Yes"<<endl;
#define no cout<<"No"<<endl;
#define flag cout<<"hi"<<endl;
#define fr(i,a,b) for(ll i = a;i < (ll)b;i++)
#define rfr(i,a,b) for(ll i = a;i > (ll)b;i--)
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define alll(x) ((x).begin()+1), (x).end()
#define MOD mod
#define endl '\n'
const ll mod = 1e9+7 ;
//void io(){ios::sync_with_stdio(false) ;cin.tie(NULL) ;freopen("grader.in.4","r",stdin) ;freopen("grader.out.4","w",stdout) ;
//}
void dbg(vector<ll> tab){for(auto it : tab) cout<<it<<" ";cout<<endl;}
void dbgg(pi p){cout<<p.F<<" "<<p.S<<endl;}
void dbgpi(vector<pi> tab){for(auto it : tab) dbgg(it) ;}
template<class T> bool ckmax(T& a, const T& b){return a < b ? a = b, 1 : 0;}
template<class T> bool ckmin(T& a, const T& b){return a > b ? a = b, 1 : 0;}
template<class T> void add(T& a, const T& b){a = a + b ; if(a>mod) a-= mod ;}
void nop(){cout<<-1<<endl;return;}
#define forr(i, x, y) for(ll i = x; i <= y; i++)
const ll inf = 1e9 +1;
const int N = 2e5+5 , MAXN = 1e5+5 , NAX = 1001 ;
int n , k ;
vector<pi> v[N] ;
int len[N] , sz[N] , vis[N] ;
void get_sz(int node , int oj)
{
sz[node] = 1 ;
for(auto [it,i] : v[node]){
if(it==oj || vis[it]) continue ;
get_sz(it,node) ;
sz[node] += sz[it] ;
}
}
int get_centro(int node , int oj , int m)
{
for(auto [it,i] : v[node]){
if(!vis[it] && it!=oj && sz[it]>m/2) return get_centro(it,node,m) ;
}
return node ;
}
int mp[N*10] , exist[N*10] ;
int ans = 1e9 ;
void get_cnt(int node , int oj , int ok , int curr_len, int d = 1)
{
if(curr_len>k) return ;
if(ok){
if(exist[k-curr_len]){
ckmin(ans, d+mp[k-curr_len]) ;
}
}
else{
if(exist[curr_len]) ckmin(mp[curr_len] , d) ;
else{
mp[curr_len] = d;
exist[curr_len] = 1 ;
}
}
for(auto [it,i] : v[node]){
if(vis[it] || it==oj) continue ;
get_cnt(it,node,ok,curr_len+len[i],d+1) ;
}
}
void del(int node , int oj , int curr_len)
{
if(curr_len>k) return ;
exist[curr_len] = 0 ;
mp[curr_len] = 1e9 ;
for(auto [it,i] : v[node]){
if(vis[it] || it==oj) continue ;
del(it,node,curr_len+len[i]) ;
}
}
void build(int node = 0)
{
get_sz(node , node) ;
int centroid = get_centro(node , node , sz[node]) ;
vis[centroid] = 1 ;
exist[0] = 1 ;
for(auto [it,i] : v[centroid]){
if(vis[it]) continue ;
get_cnt(it,centroid,1 , len[i]) ;
get_cnt(it,centroid,0 , len[i]) ;
}
for(auto [it,i] : v[centroid]){
if(vis[it]) continue ;
del(it,centroid,len[i]) ;
}
for(auto [it,i] : v[centroid]){
if(!vis[it]) build(it) ;
}
}
int best_path(int nn, int kk, int edges[][2], int weights[])
{
if(kk==1) return 0 ;
n = nn ; k = kk ;
for (ll i = 1; i < n ; i++)
{
ll x = edges[i-1][0];
ll y = edges[i-1][1];
v[x].pb(pi(y, i));
v[y].pb(pi(x, i));
len[i] = weights[i-1] ;
}
ans = 1e9 ;
build() ;
return ans >=1e9 ? -1: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... |