This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ghmt the cutie :3
UwU
*/
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define INF 1e9
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>
const int MAXN = 2e5;
int n, k;
vector<pii> g[MAXN + 5];
int h[MAXN + 5], d[MAXN + 5];
int res = INF;
map<int, int> mp[MAXN + 5];
void pre_dfs(int u, int par = -1) {
mp[u][d[u]] = h[u];
for(auto [v, w] : g[u]) {
if(v == par) continue;
h[v] = h[u] + 1;
d[v] = d[u] + w;
pre_dfs(v, u);
}
}
// mp[u][w1]
void dfs(int u, int par = -1) {
for(auto [v, w] : g[u]) {
if(v == par) continue;
dfs(v, u);
if(mp[u].size() < mp[v].size()) swap(mp[u], mp[v]);
for(auto it : mp[v]) {
int need = (k + 2 * d[u]) - it.f;
if(mp[u].find(need) != mp[u].end()) {
// cout << u << ' ' << v << ' ' << it.f << ' ' << k << ' ' << it.s + mp[u][need] - 2 * d[u] << '\n';
res = min(res, it.s + mp[u][need] - 2 * h[u]);
}
}
for(auto it : mp[v]) {
if(mp[u].find(it.f) != mp[u].end()) mp[u][it.f] = min(mp[u][it.f], it.s);
else mp[u][it.f] = it.s;
}
}
}
int solve() {
memset(h, 0, sizeof(h));
memset(d, 0, sizeof(d));
pre_dfs(1, 1);
// for(int i = 1; i <= n; i++) cout << i << ' ' << h[i] << '\n'; cout << '\n';
dfs(1, 1);
if(res == INF) return -1;
else return res;
}
int best_path(int N, int K, int H[][2], int L[])
{
// cout << "saygex\n";
n = N, k = K;
for(int i = 0; i < n - 1; i++) {
int u = H[i][0], v = H[i][1], w = L[i];
u++, v++;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
return solve();
}
# | 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... |