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>
#define range(it, a, b) for (ll it = a; it < b; it++)
#define all(x) begin(x), end(x)
#define ll long long
#define ull unsigned long long
#define INF64 ((ll) 1 << 60)
#define INF32 (1 << 30)
#define mset multiset
#define uset unordered_set
#define umap unordered_map
#define pqueue priority_queue
#define ptr(A) shared_ptr<A>
using namespace std;
struct Path {
ll i, w;
};
ll n, k;
vector<vector<Path>> adj;
vector<map<ll, ll>> paths;
ll ans = INF64;
void debug (ll i) {
cout << "NODE: " << i << '\n';
for (auto child_path : paths[i])
cout << child_path.first << " : " << child_path.second << '\n';
}
void explore(ll i, ll from, ll cnt, ll roads) {
paths[i][cnt] = roads;
for (Path child : adj[i]) {
if (child.i == from)
continue;
explore(child.i, i, cnt + child.w, roads + 1);
if (paths[i].size() < paths[child.i].size())
swap(paths[child.i], paths[i]);
for (auto child_path : paths[child.i]) {
ll sum = k + 2*cnt;
if (paths[i].count(sum - child_path.first))
ans = min(ans, paths[i][sum - child_path.first] + child_path.second - 2*roads);
}
for (auto child_path : paths[child.i]) {
if (paths[i].count(child_path.first))
paths[i][child_path.first] = min(paths[i][child_path.first], child_path.second);
else
paths[i][child_path.first] = child_path.second;
}
}
// debug(i);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N; k = K;
adj.resize(n);
paths.resize(n);
range(i, 0, n) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
explore(0, 0, 0, 0);
return (ans == INF64 ? -1 : ans);
}
# | 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... |