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;
typedef long long ll;
typedef pair<ll, ll> pii;
#define pb push_back
#define f first
#define s second
#define pi pair<int,int>
vector<pi>ad[200000];
map<int,int>m[200000];
int ans = (int)1e9,x;
void dfs(int node,int parent,int depth,int cost){
long long t = cost * 2 + x;
m[node][cost] = depth;
for (auto i : ad[node])
if(i.first != parent){
dfs(i.first,node,depth + 1,cost + i.second);
if((int)m[i.first].size() > (int)m[node].size()) m[node].swap(m[i.first]);
for (auto v : m[i.first])
if(m[node].count(t - i.second))
ans = min(ans,m[node][t - v.first] + v.second - 2 * depth);
for (auto v : m[i.first])
if(m[node].count(v.first)) m[node][v.first] = min(m[node][v.first],v.second);
else m[node][v.first] = v.second;
}
}
int best_path(int N, int K, int H[][2], int L[]){
x = K;
for (int i = 0; i < N - 1;++i){
ad[H[i][0]].push_back({H[i][1],L[i]});
ad[H[i][1]].push_back({H[i][0],L[i]});
}
dfs(1,0,0,0);
return ans == (int)1e9 ? -1 : 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... |