#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;
struct Edge {
int to, w;
};
int k;
vector<vector<Edge>> g;
vector<bool> used;
vector<int> sz;
map<int, int> mp;
int ans = inf;
void dfs_sz(int v, int p = -1) {
sz[v] = 1;
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_sz(u, v);
sz[v] += sz[u];
}
}
}
void dfs_ans(int v, int p = -1, int sum = 0, int d = 0) {
if (k - sum >= 0) {
ans = min(ans, mp[k - sum] + d);
}
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_ans(u, v, sum + w, d + 1);
}
}
}
void dfs_mp(int v, int p = -1, int sum = 0, int d = 0) {
if (mp.find(sum) == mp.end()) {
mp[sum] = d;
} else {
mp[sum] = min(mp[sum], d);
}
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
dfs_mp(u, v, sum + w, d + 1);
}
}
}
int centroid(int v, int p, int n) {
for (auto [u, w] : g[v]) {
if (u != p && !used[u]) {
if (sz[u] * 2 > n) {
return centroid(u, v, n);
}
}
}
return v;
}
void build(int v) {
dfs_sz(v);
for (auto [u, w] : g[v]) {
if (!used[u]) {
dfs_ans(u, v, w, 1);
dfs_mp(u, v, w, 1);
}
}
mp.clear();
used[v] = 1;
for (auto [u, w] : g[v]) {
if (!used[u]) {
build(centroid(u, v, sz[u]));
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
g.resize(N);
for (int i = 0; i < N - 1; ++i) {
int u = H[i][0], v = H[i][1], w = L[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
used.resize(N);
sz.resize(N);
dfs_sz(0);
build(centroid(0, -1, sz[0]));
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |