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>
#include "race.h"
using namespace std;
using lli = long long;
const int MAXN = 2e5 + 10;
const int MAXK = 1e6 + 10;
vector<pair<int, int>> graph[MAXN];
vector<int> subs[MAXN];
int sub[MAXN], dist[MAXK], aprof[MAXN];
bool marc[MAXN];
lli prof[MAXN];
void dfs_sub(int u, int p) {
sub[u] = 1;
for (auto [v, c]: graph[u]) {
if (v == p || marc[v]) continue;
dfs_sub(v, u);
sub[u] += sub[v];
}
}
void dfs_prof(int u, int p, int cor) {
subs[cor].push_back(u);
for (auto [v, c]: graph[u]) {
if (v == p || marc[v]) continue;
prof[v] = prof[u] + c;
aprof[v] = aprof[u] + 1;
dfs_prof(v, u, cor);
}
}
int dfs_cent(int u, int p, int n) {
for (auto [v, c]: graph[u]) {
if (v == p || marc[v]) continue;
if (sub[v] >= n/2) return dfs_cent(v, u, n);
}
return u;
}
int dfs(int u, int p, int n, int k) {
dfs_sub(u, p);
int c = dfs_cent(u, p, sub[u]);
marc[c] = true;
for (int i = 0; i < graph[c].size(); ++i) {
auto [v, cost] = graph[c][i];
if (!marc[v]) {
prof[v] = cost;
aprof[v] = 1;
dfs_prof(v, c, i);
}
}
int ans = 1e9;
dist[0] = 0;
for (int i = 0; i < graph[c].size(); ++i) {
for (int v: subs[i])
if (prof[v] <= k)
ans = min(ans, dist[k - prof[v]] + aprof[v]);
for (int v: subs[i])
if (prof[v] < MAXK) dist[prof[v]] = min(dist[prof[v]], aprof[v]);
}
for (int i = 0; i < graph[c].size(); ++i) {
for (int v: subs[i])
if (prof[v] < MAXK) dist[prof[v]] = 1e9;
subs[i].clear();
}
for (auto [v, cost]: graph[c])
if (!marc[v]) ans = min(ans, dfs(v, c, n, k));
return ans;
}
int best_path(int n, int k, int edges[][2], int costs[]) {
for (int i = 0; i < n-1; ++i) {
graph[edges[i][0]].emplace_back(edges[i][1], costs[i]);
graph[edges[i][1]].emplace_back(edges[i][0], costs[i]);
}
for (int i = 0; i < MAXK; ++i)
dist[i] = 1e9;
int ans = dfs(1, 1, n, k);
if (ans == 1e9) return -1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'void dfs_sub(int, int)':
race.cpp:18:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for (auto [v, c]: graph[u]) {
| ^
race.cpp: In function 'void dfs_prof(int, int, int)':
race.cpp:27:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
27 | for (auto [v, c]: graph[u]) {
| ^
race.cpp: In function 'int dfs_cent(int, int, int)':
race.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for (auto [v, c]: graph[u]) {
| ^
race.cpp: In function 'int dfs(int, int, int, int)':
race.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < graph[c].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
race.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | auto [v, cost] = graph[c][i];
| ^
race.cpp:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i < graph[c].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
race.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < graph[c].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~
race.cpp:73:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | for (auto [v, cost]: graph[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... |