이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "race.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
const ll inf = 1e9 + 10000;
const int mod = 1e9+7;
const int maxn = 2e5 + 12;
//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;
vector<pii> g[maxn];
int sz[maxn], ans = maxn, k;
bool was[maxn];
void dfs(int v, int pr) {
sz[v] = 1;
for(auto i : g[v]) {
if(!was[i.ff] && i.ff != pr) {
dfs(i.ff, v);
sz[v] += sz[i.ff];
}
}
}
int fnd(int v, int pr, int n) {
for(auto i : g[v]) {
if(!was[i.ff] && i.ff != pr){
if(sz[i.ff] > n / 2) {
return fnd(i.ff, v, n);
}
}
}
return v;
}
map<int, int> ok;
vector<pii> upd;
void cnt(int v, int pr, int x, int y) {
if(x <= k){
if(ok.find(k - x) != ok.end())ans = min(ans, ok[k - x] + y);
}
else x = k + 1;
sz[v] = 1;
for(auto i : g[v]) {
if(i.ff != pr && !was[i.ff]) {
cnt(i.ff, v, min(x + i.ss, k + 1), y + 1);
sz[v] += sz[i.ff];
}
}
upd.pb({x, y});
}
void go(int v, int n) {
dfs(v, -1);
int cen = fnd(v, -1, n);
was[cen] = 1;
ok.clear();
ok[0] = 0;
for(auto i : g[cen]){
if(!was[i.ff]){
cnt(i.ff, cen, i.ss, 1);
while(!upd.empty()){
if(ok.find(upd.back().ff) == ok.end())ok[upd.back().ff] = upd.back().ss;
else ok[upd.back().ff] = min(ok[upd.back().ff], upd.back().ss);
upd.pop_back();
}
}
}
for(auto i : g[cen]){
if(!was[i.ff]){
go(i.ff, sz[i.ff]);
}
}
}
int best_path(int n, int K, int h[][2], int l[]){
k = K;
for(int i = 0; i < n; i++ ){
g[h[i][0]].pb({h[i][1], l[i]});
g[h[i][1]].pb({h[i][0], l[i]});
}
go(0, n);
return (ans == maxn ? -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... |