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 <iostream>
#include <vector>
#include "race.h"
using namespace std;
vector<pair<int, int> > g[200001];
int sz[200001];
bool del[200001];
void rcn(int v, int pred) {
sz[v] = 1;
for (auto [to, c] : g[v]) {
if (to == pred || del[to]) continue;
rcn(to, v);
sz[v] += sz[to];
}
}
int centroid(int v, int pred, int N) {
for (auto [to, c] : g[v]) {
if (to == pred || del[to])continue;
if (sz[to] > N / 2)
return centroid(to, v, N);
}
return v;
}
int Kur;
pair<int, int> have[1000001];
int res = 1010101010;
int ID;
void dfs(int v, int pred, int sum, int depth) {
if (have[Kur - sum].first == ID)
res = min(res, have[Kur - sum].second + depth);
for (auto [to, c] : g[v]) {
if (to == pred || del[to] || sum + c > Kur)continue;
dfs(to, v, sum + c, depth + 1);
}
}
void put(int v, int pred, int sum, int depth) {
if (have[sum].first == ID)
have[sum].second = min(have[sum].second, depth);
else {
have[sum].first = ID;
have[sum].second = depth;
}
for (auto [to, c] : g[v]) {
if (to == pred || del[to] || sum + c > Kur)continue;
put(to, v, sum + c, depth + 1);
}
}
void decomposition(int v) {
del[v] = true;
rcn(v, v);
ID = v;
have[0] = {v, 0};
for (auto [to, c] : g[v]) {
if (del[to] || c > Kur)continue;
dfs(to, v, c, 1);
put(to, v, c, 1);
}
for (auto [to, c] : g[v]) {
if (del[to])continue;
decomposition(centroid(to, v, sz[to]));
}
}
int best_path(int N, int K, int H[][2], int L[]) {
for (int i = 0; i < N - 1; i++) {
g[H[i][0] + 1].push_back({H[i][1] + 1, L[i]});
g[H[i][1] + 1].push_back({H[i][0] + 1, L[i]});
}
Kur = K;
rcn(1, 1);
decomposition(centroid(1, 1, N));
// cout << centroid(1, 1, N) << " !!" << endl;
if (res == 1010101010)return -1;
return res;
}
/**
3 3
0 1 1
1 2 1
-1
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/
Compilation message (stderr)
race.cpp: In function 'void rcn(int, int)':
race.cpp:11:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
11 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'int centroid(int, int, int)':
race.cpp:18:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:32:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'void put(int, int, int, int)':
race.cpp:44:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'void decomposition(int)':
race.cpp:55:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto [to, c] : g[v]) {
| ^
race.cpp:60:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for (auto [to, c] : g[v]) {
| ^
# | 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... |