This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int addint(int a, int x) {
if (x > 0 && a > INT_MAX - x) exit(SIGSEGV);
if (x < 0 && a < INT_MIN - x) exit(SIGSEGV);
return a + x;
}
ll addll(ll a, ll x) {
if (x > 0 && a > LONG_LONG_MAX - x) exit(SIGSEGV);
if (x < 0 && a < LONG_LONG_MIN - x) exit(SIGSEGV);
return a + x;
}
ll subll(ll a, ll x) {
if (x < 0 && a > LONG_LONG_MAX + x) exit(SIGSEGV);
if (x > 0 && a < LONG_LONG_MIN + x) exit(SIGSEGV);
return a - x;
}
unordered_map<ll, int> solve(int N, ll K, vector<vector<pair<int, int>>>& es,
int& curmin, int last, int cur) {
// cout << "solve " << cur << "\n";
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(N, K, es, curmin, cur, next);
for (auto [k, v] : a) {
if (k > K) continue;
ll search = subll(K, addll(k, len));
auto it = ans.find(search);
if (it != ans.end()) {
curmin = min(curmin, addint(it->second, addint(v, 1)));
}
}
for (auto [k, v] : a) {
if (k > K) continue;
ans.insert({addll(k, len), addint(v, 1)});
}
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]) {
// cout << "testcase\n";
vector<vector<pair<int, int>>> es(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]});
}
int curmin = N;
auto ans = solve(N, K, es, curmin, -1, 0);
if (curmin == N) {
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... |