이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
// cout << "RET: " << v << " " << u.ff << END;
// for (auto u: ret) cout << u.ff << " " << u.ss << END;
// cout << END;
if (mp.size() < ret.size()) swap(mp, ret);
for (auto x : ret) {
if (mp.find(k - x.ff + l * 2) != mp.end()) {
int pp = mp[k - x.ff + l * 2];
// cout << "In " << v << ": " << u.ff << " " << x.ff << " " << x.ss << " " << k - x.ff + l * 2 << " " << pp << " " << d << " " << l << END;
ans = min(ans, pp + x.ss - 2 * d);
}
}
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];
// cout << "In " << v << ": " << u.ff << " " << x.ff << " " << x.ss << " " << k - x.ff + l * 2 << " " << pp << " " << d << " " << l << END;
// 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... |