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;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define END "\n"
#define rall(v) (v).rbegin(), (v).rend()
#define all(v) (v).begin(), (v).end()
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int best_path(int n, int k, int H[][2], int L[]) {
vector<vector<pii>> adj(n);
for (int i = 0; i < n - 1; ++i) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
int ans = INT_MAX;
function<map<ll, int>(int, int, int, ll)> dfs = [&](int v, int p, int d, ll l) -> map<ll, int> {
map<ll, int> mp;
mp[l] = d;
for (auto u : adj[v]) {
if (u.ff ^ p) {
auto ret = dfs(u.ff, v, d + 1, l + u.ss);
if (mp.size() < ret.size()) swap(mp, ret);
for (auto x : ret) {
// k = a + b - v * 2
// cout << "In " << v << ": " << u.ff << " " << x.ff << " " << x.ss << END;
if (mp.find(k - x.ff + l * 2) != mp.end()) {
int pp = mp[k - x.ff + l * 2];
ans = min(ans, pp + x.ss - 2 * d);
}
if (mp.find(x.ff) == mp.end())
mp[x.ff] = x.ss;
else
mp[x.ff] = min(mp[x.ff], x.ss);
}
}
}
return mp;
};
dfs(0, -1, 0, 0);
return ans == INT_MAX ? -1 : ans;
// return 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... |