This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 2e5+10;
const int mxK = 1e6+10;
const int INF = 1e9+10;
vector<array<int, 2>> adj[mxN];
int bk = 0, k, ans = INF;
int sz[mxN], d[mxN], last[mxN];
bool vis[mxN];
void get_sz(int node, int p) {
sz[node] = 1;
for(auto [it, w] : adj[node]) {
if(it == p || vis[it]) continue;
get_sz(it, node);
sz[node] += sz[it];
}
}
int get_c(int node, int p, int n) {
for(auto [it, w] : adj[node]) {
if(vis[it] || it == p) continue;
if(sz[it] * 2 > n) return get_c(it, node, n);
}
return node;
}
void get_dist(int node, int p, bool filling, int dist, int s) {
if(s > k) return;
if(filling) {
if(last[s] != bk) {
d[s] = dist;
last[s] = bk;
}
else {
d[s] = min(d[s], dist);
}
}
else {
if(last[k-s] == bk) {
ans = min(ans, dist+d[k-s]);
}
}
for(auto [it, w] : adj[node]) {
if(it == p || vis[it]) continue;
get_dist(it, node, filling, dist+1, s+w);
}
}
void build(int node) {
get_sz(node, -1);
int c = get_c(node, -1, sz[node]);
vis[c] = true;
++bk;
d[0] = bk;
for(auto [it, w] : adj[c]) {
if(vis[it]) continue;
get_dist(it, c, 0, 1, w);
get_dist(it, c, 1, 1, w);
}
for(auto [it, w] : adj[c]) {
if(vis[it]) continue;
build(it);
}
}
int best_path(int n, int K, int h[][2], int l[]) {
for(int i = 0; i < n-1; i++) {
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
k = K;
build(0);
return (ans == INF ? -1 : ans);
}
/*
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << best_path(11, 12, {{0, 1}, {0, 2}, {2, 3}, {3, 4}, {4, 5}, {0, 6},
{6, 7}, {6, 8}, {8, 9}, {8, 10}},
{3, 4, 5, 4, 6, 3, 2, 5, 6, 7});
}*/
Compilation message (stderr)
race.cpp: In function 'void get_sz(int, int)':
race.cpp:16:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for(auto [it, w] : adj[node]) {
| ^
race.cpp: In function 'int get_c(int, int, int)':
race.cpp:24:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
24 | for(auto [it, w] : adj[node]) {
| ^
race.cpp: In function 'void get_dist(int, int, bool, int, int)':
race.cpp:47:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
47 | for(auto [it, w] : adj[node]) {
| ^
race.cpp: In function 'void build(int)':
race.cpp:59:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | for(auto [it, w] : adj[c]) {
| ^
race.cpp:64:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | for(auto [it, w] : adj[c]) {
| ^
# | 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... |