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 <bits/stdc++.h>
#include "race.h"
typedef long long int ll;
#define INF (int)(1e9 + 7)
#define INF2 998244353
#define N (ll)(2e5 + 5)
using namespace std;
//#define int ll
#define lsb(x) (x & (-x))
int n, k;
ll g;
ll ans = INF;
vector<pair<int, ll>> v[N];
set<pair<ll, int>> s[N];
ll d[N];
ll dis[N];
void dfs(int x, int p){
for(auto u : v[x]){
if(u.first != p){
d[u.first] = d[x] + 1;
dis[u.first] = dis[x] + u.second;
dfs(u.first, x);
}
}
s[x].insert({dis[x], d[x]});
for(auto u : v[x]){
if(u.first != p){
if(s[x].size() < s[u.first].size()){
swap(s[x], s[u.first]);
}
for(auto j : s[u.first]){
g = k - (j.first - dis[x]) + dis[x];
auto it = s[x].lower_bound(make_pair(g, 0ll));
if(it != s[x].end() && it->first == g){
ans = min(ans, j.second + it->second - 2 * d[x]);
}
s[x].insert(j);
}
}
}
s[x].insert({0, d[x]});
}
int best_path(int nn, int K, int H[][2], int L[]){
n = nn;
for(int i=0; i<n-1; i++){
v[H[i][0]].push_back({H[i][1], L[i]});
v[H[i][1]].push_back({H[i][0], L[i]});
}
k=K;
dfs(0, -1);
if(ans == INF)return -1;
return 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... |