# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
988877 | NoLove | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author : Lăng Trọng Đạt
* created: 26-05-2024
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
using vi = vector<int>;
#define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
void mx(int& a, int b) { if (b > a) a = b; }
void mi(int& a, int b) { if (b < a) a = b; }
#define si(x) (int)(x.size())
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 5e5 + 5;
int g[MAXN];
int n, ans = INF, k;
vector<pii> adj[MAXN];
map<int, int> md[MAXN]; // md[v][l] = độ sâu lớn nhất khi đi khoảng cách từ node đó đến root đúng = l
void dfs(int v, int prv, int len, int d) {
md[v][len] = d;
for (auto[u, l] : adj[v]) {
if (u == prv) continue;
dfs(u, v, len + l, d + 1);
if (si(md[u]) > si(md[v])) swap(md[u], md[v]);
// x + y - 2 * len = k
// length = depth + depth_y - 2*d
for (auto[x, depth] : md[u]) {
if (md[v].count(k + 2*len - x)) {
mi(ans, depth + md[v][k + 2*len - x] - 2*d);
}
}
for (auto[x, depth] : md[u]) {
if (md[v].count(x)) {
mi(md[v][x], depth);
} else {
md[v][x] = depth;
}
}
}
db(v, md[v])
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
FOR(i, 0, N - 2) {
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
dfs(1, -1, 0, 0);
db(ans);
return (ans == INF ? -1 : ans);
}