이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include <map>
#include <sstream>
#include <stack>
#include <bitset>
using ll = long long;
using ld = long double;
using namespace std;
const int nm = 200002;
set<pair<int, int>> adj[nm];
int H[nm][2], L[nm];
int Res;
unordered_map<int, int> bfs(int i, int p, int k) {
queue<int> q;
q.push(i);
unordered_map<int, pair<int, int>> d;
d[i] = {0, 0};
unordered_map<int, int> res;
res[0] = 0;
while (q.size()) {
int i = q.front();
q.pop();
for (auto [j, w]: adj[i]) {
if (j == p) continue;
if (!d.count(j)) {
d[j].first = d[i].first + w;
d[j].second = d[i].second + 1;
if (d[j].first <= k) {
if (!res.count(d[j].first) || res[d[j].first] > d[j].second) res[d[j].first] = d[j].second;
if (d[j].second < Res && d[j].first < k) q.push(j);
}
}
}
}
return res;
}
void check(int root, int k) {
unordered_map<int, int> res;
for (auto [j, w]: adj[root]) {
auto resj = bfs(j, root, k);
for (auto [cost, len]: resj) {
if (cost + w > k) continue;
if (cost + w == k) {
Res = min(Res, len + 1);
} else if (res.count(k - cost - w)) {
Res = min(Res, res[k - cost - w] + len + 1);
}
}
for (auto [cost, len]: resj) {
if (cost + w > k) continue;
if (!res.count(cost + w) || res[cost + w] > len + 1) {
res[cost + w] = len + 1;
}
}
}
}
void dfs(int i, int p, unordered_map<int, pair<int, int>> &child) {
int nchild = 1;
int maxchild = 0;
for (auto [j, w]: adj[i]) {
if (j == p) continue;
dfs(j, i, child);
nchild += child[j].first;
maxchild = max(maxchild, child[j].first);
}
child[i] = {nchild, maxchild};
}
int findCenter(int root) {
unordered_map<int, pair<int, int>> child;
dfs(root, -1, child);
int s = child[root].first;
if (s == 1) return -1;
for (auto [i, p]: child) {
int maxsplit = max(p.second, s - p.first);
if (maxsplit <= s / 2) return i;
}
return -1;
}
void remove(int u) {
for (auto [j, w]: adj[u]) {
adj[j].erase({u, w});
}
}
void calc(int root, int k) {
int u = findCenter(root);
// cout << "root " << root << " center " << u << "\n";
if (u == -1) return;
check(u, k);
// cout << "root " << root << " center " << u << " " << Res << "\n";
remove(u);
for (auto [j, w]: adj[u]) {
calc(j, k);
}
}
int best_path(int n, int k, int H[][2], int L[]) {
for (int i = 0; i < n - 1; ++i) {
adj[H[i][0]].emplace(H[i][1], L[i]);
adj[H[i][1]].emplace(H[i][0], L[i]);
}
Res = n;
calc(0, k);
return Res < n ? Res : -1;
}
// int main() {
// #ifdef LOCAL
// freopen("input.txt", "r", stdin);
// #endif
// ios::sync_with_stdio(0);
// cin.tie(0);
// 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";
// }
# | 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... |