이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |