이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
// #include <sys/resource.h>
typedef long long ll;
int GN, GK;
vector<vector<pair<int, int>>> es;
int curmin;
vector<int> parent;
unordered_map<ll, int> solve(int last, int cur) {
// cout << "solve " << cur << endl;
unordered_map<ll, int> ans;
ans.insert({0, 0});
for (auto [next, len] : es[cur]) {
if (next == last) continue;
unordered_map<ll, int> a = solve(cur, next);
for (auto [k, v] : a) {
if (k > GK) continue;
ll search = GK - (k + (ll)len);
auto it = ans.find(search);
if (it != ans.end()) {
curmin = min(curmin, it->second + v + 1);
}
}
for (auto [k, v] : a) {
if (k > GK) continue;
ans.insert({k + (ll)len, v + 1});
}
}
return ans;
}
vector<unordered_map<ll, int>> ans1;
void solve1(int cur) {
unordered_map<ll, int>& ans = ans1[cur];
ans.insert({0, 0});
for (auto [next, len] : es[cur]) {
if (next == parent[cur]) continue;
auto& a = ans1[next];
for (auto [k, v] : a) {
if (k > GK) continue;
ll search = GK - (k + (ll)len);
auto it = ans.find(search);
if (it != ans.end()) {
curmin = min(curmin, it->second + v + 1);
}
}
for (auto [k, v] : a) {
if (k > GK) continue;
ans.insert({k + (ll)len, v + 1});
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
GN = N;
GK = K;
// rlimit rl;
// getrlimit(RLIMIT_STACK, &rl);
// rl.rlim_cur =
// max(rl.rlim_cur,
// (sizeof(unordered_map<ll, int>) + sizeof(int) * 2 + 20) *
// 2000100);
// setrlimit(RLIMIT_STACK, &rl);
// cout << "doit " << N << endl;
es = vector<vector<pair<int, int>>>(N);
for (int i = 0; i < N - 1; i++) {
es[H[i][0]].push_back({H[i][1], L[i]});
es[H[i][1]].push_back({H[i][0], L[i]});
}
parent = vector<int>(N, -1);
vector<int> stack;
stack.push_back(0);
vector<int> topo;
while (stack.size()) {
int cur = stack.back();
stack.pop_back();
topo.push_back(cur);
for (auto [e, l] : es[cur]) {
if (e != parent[cur]) {
parent[e] = cur;
stack.push_back(e);
}
}
}
curmin = INT_MAX;
ans1 = vector<unordered_map<ll, int>>(N);
for (int i = topo.size() - 1; i >= 0; i--) {
solve1(i);
}
// auto ans = solve(-1, 0);
if (curmin == INT_MAX) {
return -1;
} else {
return curmin;
}
}
# | 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... |