이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
const int maxn = 2e5 + 5, inf = 1e9;
vector<pair<int, int>> adj[maxn];
long long sol = inf, k, sub[maxn];
bool visited[maxn];
unordered_map<long long, long long> occ;
int find_centroid(int c, int p, long long s) {
for (auto &i : adj[c]) {
if (i.first != p && !visited[i.first] && sub[i.first] > s / 2) {
return find_centroid(i.first, c, s);
}
}
return c;
}
int find_size(int c, int p) {
sub[c] = 1;
for (auto &i : adj[c]) {
if (i.first != p && !visited[i.first]) {
sub[c] += find_size(i.first, c);
}
}
return sub[c];
}
void dfs(int c, int p, bool add, long long dist, long long depth) {
if (dist > k) {
return;
}
#ifdef DEBUG
cout << c << " " << p << " " << dist << " " << k - dist << " " << depth << "\n";
#endif
if (add) {
if (occ.find(k - dist) != occ.end()) {
#ifdef DEBUG
cout << c << " " << p << " " << dist << " " << k - dist << " " << depth << " " << occ[k - dist] << "\n";
#endif
sol = min(sol, occ[k - dist] + depth);
}
} else {
if (occ.find(dist) == occ.end()) {
occ[dist] = depth;
} else {
occ[dist] = min(occ[dist], depth);
}
}
for (auto &i : adj[c]) {
if (i.first != p && !visited[i.first]) {
dfs(i.first, c, add, dist + i.second, depth + 1);
}
}
}
void solve(int v) {
find_size(0, -1);
int c = find_centroid(v, -1, sub[v]);
#ifdef DEBUG
cout << "c: " << c << endl;
#endif
visited[c] = true;
for (auto &i : adj[c]) {
if (!visited[i.first]) {
dfs(i.first, c, true, i.second, 1);
dfs(i.first, c, false, i.second, 1);
}
}
if (occ.find(k) != occ.end()) {
sol = min(sol, occ[k]);
}
occ.clear();
for (auto &i : adj[c]) {
if (!visited[i.first]) {
solve(i.first);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
for (int i = 0; i < N - 1; ++i) {
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
solve(0);
return sol == inf ? -1 : sol;
}
#ifdef LOCAL
int h[maxn][2], l[maxn];
int main() {
freopen("grader.in.5", "r", stdin);
freopen("out.txt", "w", stdout);
int n, K;
cin >> n >> K;
for (int i = 0; i < n - 1; ++i) {
cin >> h[i][0] >> h[i][1] >> l[i];
}
cout << best_path(n, K, h, l) << "\n";
}
#endif
# | 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... |