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"
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
int ans = 1e9, k;
vector<vector<pii> > graph;
vector<int> dist, edges;
vector<map<int, int> > sets;
void precomp(int u, int p, int e, int d) {
edges[u] = e;
dist[u] = d;
for(auto &[v, w] : graph[u]) {
if(v == p) continue;
precomp(v, u, e + 1, d + w);
}
}
//dist[u] + dist[v] - 2 * dist[lca] = K;
void dfs(int u, int p) {
sets[u][dist[u]] = edges[u];
for(auto &[v, w] : graph[u]) {
if(v == p) continue;
dfs(v, u);
if(sz(sets[u]) < sz(sets[v])) swap(sets[u], sets[v]);
for(auto &x : sets[v])
if(sets[v].count(k + 2 * dist[u] - x.first))
ans = min(ans, x.second + sets[v][k+2*dist[u]-x.first] - 2 * edges[u]);
for(auto &x : sets[v]) {
if(sets[u].count(x.first)) sets[u][x.first] = min(sets[u][x.first], x.second);
else sets[u][x.first] = x.second;
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
graph.resize(N);
dist.resize(N);
edges.resize(N);
sets.resize(N);
for(int i=0; i<N-1; i++) {
graph[H[i][0]].push_back({ H[i][1], L[i] });
graph[H[i][1]].push_back({ H[i][0], L[i] });
}
precomp(0, 0, 0, 0);
dfs(0, 0);
return (ans == 1e9 ? -1 : ans);
}
// int32_t main() {
// int n, k;
// cin >> n >> k;
// int g[n-1][2];
// for(int i=0; i<n-1; i++) {
// int a, b;
// cin >> a >> b;
// g[i][0] = a;
// g[i][1] = b;
// }
// int len[n-1];
// for(int i=0; i<n-1; i++) cin >> len[i];
// cout << best_path(n, k, g, len) << '\n';
// return 0;
// }
Compilation message (stderr)
race.cpp: In function 'void precomp(int, int, int, int)':
race.cpp:34:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto &[v, w] : graph[u]) {
| ^
race.cpp: In function 'void dfs(int, int)':
race.cpp:43:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | for(auto &[v, w] : graph[u]) {
| ^
# | 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... |