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 <vector>
using namespace std;
#define oo 1000000000
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
int ans = oo;
vector<vector<pii>> ed;
vector<bool> used;
vector<int> size;
vector<pii> v;
void calcSize(int cur, int pr){
size[cur] = 1;
for (auto [x, w] : ed[cur]){
if (used[x] || x == pr) continue;
calcSize(x, cur);
size[cur] += size[x];
}
}
int findCentroid(int cur, int pr, int n){
for (auto [x, w] : ed[cur]){
if (used[x] || x == pr) continue;
if (size[x] > n / 2){
return findCentroid(x, cur, n);
}
}
return cur;
}
void dfs(int cur, int pr, int k, int d, int l){
if (d > k) return;
v.pb({d, l});
for (auto [x, w] : ed[cur]){
if (x == pr || used[x]) continue;
dfs(x, cur, k, d + w, l + 1);
}
}
void calc(int cur, int n, int k){
calcSize(cur, -1);
cur = findCentroid(cur, -1, n);
vector<int> dp(k+1, oo);
used[cur] = true;
for (auto [x, w] : ed[cur]){
v.clear();
dfs(x, cur, k, w, 1);
for (auto [d, l] : v){
ans = min(ans, l + dp[k - d]);
}
for (auto [d, l] : v){
dp[d] = min(dp[d], l);
}
}
for (auto [x, w] : ed[cur]){
if (used[x]) continue;
calc(x, n, k);
}
}
int best_path(int n, int k, int h[][2], int l[]){
ed.resize(n);
used.resize(n);
size.resize(n);
for (int i = 0; i < n - 1; ++i) {
ed[h[i][0]].pb({h[i][1], l[i]});
ed[h[i][1]].pb({h[i][0], l[i]});
}
calc(0, n, k);
if (ans == oo) ans = -1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'void calcSize(int, int)':
race.cpp:20:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [x, w] : ed[cur]){
| ^
race.cpp: In function 'int findCentroid(int, int, int)':
race.cpp:28:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
28 | for (auto [x, w] : ed[cur]){
| ^
race.cpp: In function 'void dfs(int, int, int, int, int)':
race.cpp:40:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
40 | for (auto [x, w] : ed[cur]){
| ^
race.cpp: In function 'void calc(int, int, int)':
race.cpp:53:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
53 | for (auto [x, w] : ed[cur]){
| ^
race.cpp:57:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for (auto [d, l] : v){
| ^
race.cpp:60:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for (auto [d, l] : v){
| ^
race.cpp:65:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | for (auto [x, w] : ed[cur]){
| ^
# | 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... |