이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
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);
}
}
int kocot(int ter,int per){
int kos=1;
if(used[ter]){return 1;}
for(auto it:road[ter]){
if(it.fir==per){continue;}
kos+=kocot(it.fir,ter);
}
return kos;
}
void solve(int ter){
//terを含む重心を見つけて行います
if(used[ter]){return;}
mapban--;
mymap[0]=mp(mapban,0);
nozen=kocot(ter,-1);
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));}
}
for(auto it:road[zyu]){solve(it.fir);}
}
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);
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... |