이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include "race.h"
using namespace std;
#define ll long long
#define ff first
#define ss second
#define ln "\n"
vector<pair<ll, pair<ll, ll>>> e;
vector<vector<ll>> A;
vector<bool> black;
vector<ll> sz;
ll n, k;
void gensz(ll u, ll p){
sz[u]=1;
for (auto i:A[u]){
ll v = e[i].ss.ff^e[i].ss.ss^u;
if (v==p) continue;
gensz(v, u);
sz[u]+=sz[v];
}
}
void findcentroid(ll u, ll p, ll up, ll ¢r, ll wsz){
bool pos=(up<=wsz/2);
for (auto i:A[u]){
if (black[i]) continue;
ll v = e[i].ss.ff^e[i].ss.ss^u;
if (v==p) continue;
if (sz[v]>wsz/2) pos=0;
findcentroid(v, u, up+1, centr, wsz);
}
if (pos) centr=u;
}
void dfs(ll u, ll p, map<ll, ll> &usg, ll level, ll dep){
if (usg.count(level)) usg[level]=min(usg[level], dep);
else usg[level]=dep;
for (auto i:A[u]){
ll v = e[i].ss.ff^e[i].ss.ss^u;
if (v==p or black[i]) continue;
dfs(v, u, usg, level+e[i].ff, dep+1);
}
}
void cedfs(ll u, ll &mn, ll wsz){
ll centr = -1;
findcentroid(u,u, 0, centr, wsz);
assert(centr!=-1);
map<ll, ll> full, levs;
for (auto i:A[u]){
if (black[i]) continue;
ll v = e[i].ss.ff^e[i].ss.ss^u;
dfs(v, v, levs, 0, 0);
for (auto [length, tim]:levs){
// cout << length << "-" << tim << ' ';
if (full.count(k-length-e[i].ff)){
mn=min(mn, full[k-length-e[i].ff]+tim+1);
}
if (length+e[i].ff==k){
mn=min(mn, tim+1);
}
}
// cout << ln;
for (auto [length, tim]:levs){
if (full.count(length+e[i].ff)){
full[length+e[i].ff]=min(full[length+e[i].ff], tim+1);
}else{
full[length+e[i].ff]=tim+1;
}
}
}
for (auto i:A[u]){
if (black[i]) continue;
black[i]=1;
ll v = e[i].ss.ff^e[i].ss.ss^u;
cedfs(v, mn, sz[v]);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N; k=K;
e.resize(N-1);
A.resize(N);
for (ll i=0; i<n-1; i++){
A[H[i][0]].push_back(i);
A[H[i][1]].push_back(i);
e[i] = {L[i], {H[i][0], H[i][1]}};
}
black.resize(N);
sz.resize(N);
ll mn = 2e18;
gensz(0, 0);
cedfs(0, mn, sz[0]);
return (mn==2e18?-1:mn);
}
# | 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... |