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<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=1000000000;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
//マージテク!
//重心分解!!!
//nlogn!!!!!!
int ans=869120;
int K;
vector<vector<pair<int,int>>>road;
vector<bool>used;
vector<pair<int,int>>mymap;//バージョン、道路数
vector<pair<int,int>>tuivec;//長さ、道路数
int mapban=0;
int nozen;
pair<int,int>gezyu;
vector<pair<int,int>>kizen;
int fizyu(int ter,int per){
int kos=1,dka=0;
if(used[ter]){return 1;}
for(auto it:road[ter]){
if(it.fir==per){continue;}
int sor=fizyu(it.fir,ter);
if(per==-1){kizen.pub(mp(it.fir,sor));}
maxeq(dka,sor);
kos+=sor;
}
maxeq(dka,nozen-kos);
mineq(gezyu,mp(dka,ter));
return kos;
}
void dfs(int ter,int per,int dis,int num){
tuivec.pub(mp(dis,num));
if(mymap[K-dis].fir==mapban){mineq(ans,num+mymap[K-dis].sec);}
if(used[ter]){return;}
for(auto it:road[ter]){
if(it.fir==per||dis+it.sec>K){continue;}
dfs(it.fir,ter,dis+it.sec,num+1);
}
}
void solve(int ter,int zen){
//terを含む重心を見つけて行います
if(used[ter]){return;}
mapban--;
mymap[0]=mp(mapban,0);
nozen=zen;
gezyu=mp(869120,ter);
fizyu(ter,-1);
int zyu=gezyu.sec;//重心に対して何かする
used[zyu]=1;
for(auto it:road[zyu]){
tuivec.clear();
if(it.sec<K){dfs(it.fir,zyu,it.sec,1);}
for(auto tui:tuivec){mineq(mymap[tui.fir],mp(mapban,tui.sec));}
}
auto myzen=kizen;
for(auto it:myzen){solve(it.fir,it.sec);}
}
int best_path(int n,int KK,int H[][2],int L[]){
tuivec.resize(n);
K=KK;
road.res(n);
used.res(n);
mymap.res(K+1);
int i;
for(i=0;i<n-1;i++){
if(L[i]==K){return 1;}
road[H[i][0]].pub(mp(H[i][1],L[i]));
road[H[i][1]].pub(mp(H[i][0],L[i]));
}
solve(0,n);
if(ans==869120){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... |