This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAX_N = 2e5 + 5;
const int MAX_K = 1e6 + 5;
vector<vector<pair<int, int>>> g(MAX_N);
vector<int> sub(MAX_N), depth(MAX_N);
vector<long long> dist(MAX_N);
int ans = 1e9, _K;
void dfs(int node, int p) {
sub[node] = 1;
for (auto to : g[node]) {
if (to.first != p) {
depth[to.first] = depth[node] + 1;
dist[to.first] = dist[node] + to.second;
dfs(to.first, node);
sub[node] += sub[to.first];
}
}
}
unordered_map<long long, multiset<int> > best_way;
long long lca = -1;
int lca_d = -1;
void update_ans(long long d, int w) {
long long t = _K - d + 2 * lca;
if (t < 0) return;
if (best_way[t].empty()) return;
int where = *best_way[t].begin();
ans = min (ans, w + where - 2 * lca_d);
}
void doit(int node, int p, bool task, bool act_add) {
if (task) {
if (!act_add) update_ans(dist[node], depth[node]);
else best_way[dist[node]].insert(depth[node]);
} else {
best_way[dist[node]].erase(best_way[dist[node]].lower_bound(depth[node]));
}
for (auto to : g[node]) if (to.first != p) doit(to.first, node, task, act_add);
}
void solve(int node, int p, bool keep) {
int mx = -1, big = -1;
for (auto to : g[node]) if (to.first != p && sub[to.first] > mx) mx = sub[to.first], big = to.first;
for (auto to : g[node]) if (to.first != p && to.first != big) solve(to.first, node, false);
if (~big) solve(big, node, true);
lca = dist[node];
lca_d = depth[node];
update_ans(dist[node], depth[node]);
best_way[dist[node]].insert(depth[node]);
for (auto to : g[node]) {
if (to.first != p && to.first != big) {
doit(to.first, node, true, false);
doit(to.first, node, true, true);
}
}
if (!keep) {
best_way[dist[node]].erase(best_way[dist[node]].lower_bound(depth[node]));
for (auto to : g[node]) if (to.first != p) doit(to.first, node, false, false);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
ios::sync_with_stdio(0);
cin.tie(0);
_K = K;
for (int i = 0; i < N - 1; i++) {
int st = H[i][0];
int et = H[i][1];
int w = L[i];
g[st].emplace_back(et, w);
g[et].emplace_back(st, w);
}
dfs(0, -1);
solve(0, -1, false);
return (ans > 1e8 ? -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... |