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"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int INF = 1e9;
const int N = 2e5+5;
int ret, n, k;
vector<pii> adj[N];
map<int, int> mp[N];
void dfs(int x, int pr, int d, int lvl){
mp[x][d] = lvl;
tr(it, adj[x])
if(it->ff!=pr){
dfs(it->ff, x, d+it->ss, lvl+1);
if((int)mp[it->ff].size()>(int)mp[x].size())
swap(mp[x], mp[it->ff]);
for(auto &i : mp[it->ff])
if(mp[x].find(k+2*d-i.ff)!=mp[x].end())
umin(ret, mp[x][k+2*d-i.ff]+i.ss-2*lvl);
for(auto &i : mp[it->ff]){
if(mp[x].find(i.ff)==mp[x].end())
mp[x][i.ff]=i.ss;
umin(mp[x][i.ff], i.ss);
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
for(int i = 0; i < n-1; ++i)
adj[H[i][0]].pb({H[i][1], L[i]}), adj[H[i][1]].pb({H[i][0], L[i]});
ret = INF;
dfs(0, -1, 0, 0);
return (ret==INF?-1:ret);
}
# | 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... |