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 "race.h"
#include <bits/stdc++.h>
#define N 200200
using namespace std;
vector<pair<int, int> > g[N];
int d[N];
//int p[N];
int get_centroid(int v, int parent, int n, int ¢roid) {
int size = 1;
for (auto[x, y]:g[v]) {
if (x != parent && d[x] == -1) {
size += get_centroid(x, v, n, centroid);
}
}
if (size * 2 >= n && centroid == -1) centroid = v;
return size;
}
int K;
int ans = 1e9;
void get_compo_dists(int v, int dist, int len, int pass_limit, vector<pair<int, int> > &vec, int p = -1)
{
if (dist > K) return;
vec.emplace_back(dist, len);
for (auto[x, y]:g[v])
if ((d[x] > pass_limit || d[x] == -1) && p != x)
get_compo_dists(x, dist + y, len + 1, pass_limit, vec, v);
}
void build(int v, int parent, int depth, int n)
{
int centroid = -1;
get_centroid(v, -1, n, centroid);
if (centroid == -1) centroid = v;
d[centroid] = depth;
// p[centroid] = parent;
map<int, int> dist;
dist[0] = 0;
for (auto[x, y]:g[centroid])
if (d[x] == -1)
{
vector<pair<int, int>> vec;
get_compo_dists(x, y, 1, d[centroid], vec);
for (auto[distance, cost]: vec)
if (dist.count(K - distance))
ans = min(ans, cost + dist[K - distance]);
for (auto[distance, cost]: vec)
if (dist.count(distance) == 0)
dist[distance] = cost;
else dist[distance] = min(dist[distance], cost);
build(x, centroid, depth + 1, (n + 1) / 2);
}
}
int best_path(int n, int K, int H[][2], int L[])
{
::K = K;
for (int i = 0; i < n - 1; i++)
{
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
fill(d, d + N, -1);
build(0, -1, 1, n);
return (ans == 1e9 ? -1 : ans);
}
Compilation message (stderr)
race.cpp: In function 'int get_centroid(int, int, int, int&)':
race.cpp:13:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
13 | for (auto[x, y]:g[v]) {
| ^
race.cpp: In function 'void get_compo_dists(int, int, int, int, std::vector<std::pair<int, int> >&, int)':
race.cpp:30:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
30 | for (auto[x, y]:g[v])
| ^
race.cpp: In function 'void build(int, int, int, int)':
race.cpp:45:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
45 | for (auto[x, y]:g[centroid])
| ^
race.cpp:51:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | for (auto[distance, cost]: vec)
| ^
race.cpp:55:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto[distance, cost]: vec)
| ^
# | 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... |