이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
const int N = (int)2e5 + 9;
const int MX = (int)1e6 + 9;
const int inf = (int)1e9 + 1;
int K;
int bes[MX];
vector<pii> T[N];
bool use[MX];
int subt[N];
int res = inf;
void dfs1(int u, int par){
subt[u] = 1;
for(auto x : T[u]){
if(use[x.fi] || x.fi == par) continue;
dfs1(x.fi, u);
subt[u] += subt[x.fi];
}
}
vector<pii> dd;
void dfs2(int u, int par, int sd, int ln){
if(sd > K)
return;
dd.push_back(mp(sd, ln));
for(auto x : T[u]){
if(use[x.fi] || x.fi == par) continue;
dfs2(x.fi, u, sd + x.se, ln + 1);
}
}
void split(int node){
dfs1(node, -1);
int par = -1;
int atl = subt[node] / 2;
bool ok = true;
while(ok){
ok = false;
for(auto x : T[node]){
if(x.fi == par || use[x.fi])continue;
if(subt[x.fi] > atl){
par = node;
node = x.fi;
ok = true;
break;
}
}
}
bes[0] = 0;
vector<int> er;
for(auto x : T[node]){
if(use[x.fi]) continue;
dd.clear();
dfs2(x.fi, node, x.se, +1);
for(auto p : dd){
res = min(res, p.se + bes[K - p.fi]);
er.push_back(p.fi);
}
for(auto p : dd){
bes[p.fi] = min(bes[p.fi], p.se);
}
}
for(auto x : er)
bes[x] = inf;
use[node] = true;
for(auto x : T[node]){
if(!use[x.fi])
split(x.fi);
}
}
int best_path(int n, int k, int h[][2], int ln[]){
K = k;
for(int i = 0 ;i < MX; i ++ )
bes[i] = inf;
for(int i = 0 ; i < n- 1; i ++ ){
T[h[i][0]].push_back(mp(h[i][1], ln[i]));
T[h[i][1]].push_back(mp(h[i][0], ln[i]));
}
split(0);
return (res == inf ? -1 : res);
}
# | 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... |