이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
Free at last, Free at last, Thank God Almighty, We are free at last.
*/
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#define ll long long
#define nl cout << '\n'
#define pri(n) cout<<fixed<<setprecision(n)
#define d5l_arr(arr, n) for(int i = 0 ; i < n ; i ++)cin >> arr[i]
const
double eps = -1e-9;
//const long double pi = 3.14159265358979323846;
const ll MOD = 1e9 + 7;
const int inf = 1e9 + 1;
const int N = 2e5 + 1;
void m4_htgeb_tle_kda_ya3ny() {
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
cout.sync_with_stdio(0);
}
int ans = inf, n, k;
vector<pair<int, int>> adj[N];
map<ll, int> dfs(int u, int p, ll w, int dep) {
map<ll, int> leader, other_leader;
leader[w] = dep;
for (auto v: adj[u]) {
if (v.first == p)continue;
other_leader = dfs(v.first, u, w + v.second, dep + 1);
if (other_leader.size() > leader.size())swap(leader, other_leader);
if (other_leader.count(k + w))ans = min(ans, (other_leader[k + w] - dep));
for (auto el: other_leader) {
ll rem = el.first - w;
ll look_for = (k - rem) + w;
if (leader.count(look_for))ans = min(ans, (el.second - dep) + (leader[look_for] - dep));
}
for (auto el: other_leader) {
if (leader.count(el.first))leader[el.first] = min(leader[el.first], el.second);
else leader.insert(el);
}
other_leader.clear();
}
return leader;
}
int solveCase() {
dfs(0, 0, 0, 0);
return ((ans < inf) ? ans : -1);
}
int best_path(int nn, int kk, int h[][2], int l[]){
n = nn, k = kk;
vector<int> edges[n - 1];
for (int i = 0; i < n - 1; ++i) {
int u = h[i][0], v = h[i][1];
edges[i].push_back(u);
edges[i].push_back(v);
}
for (int i = 0; i < n - 1; ++i) {
int w = l[i];
adj[edges[i][0]].emplace_back(edges[i][1], w);
adj[edges[i][1]].emplace_back(edges[i][0], w);
}
return solveCase();
}
| # | 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... |