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 "race.h"
//Dost SEFEROĞLU
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
#define all(xx) xx.begin(),xx.end()
#define ps(xxx) cout << (xxx) << endl;
const int LIM = 2e5+1,inf = 1e18;
vector<pii> edges[LIM];
map<int,int> s[LIM];
int ans = inf;
int k;
void dfs(int node,int p,int d = 0,int dep = 0) {
for (auto it : edges[node]) if (it.ss != p) dfs(it.ss,node,d+it.ff,dep+1);
s[node][d] = dep;
for (auto it : edges[node]) {
if (it.ss == p) continue;
if (s[it.ss].size() > s[node].size()) s[it.ss].swap(s[node]);
for (auto it : s[it.ss]) {
if (s[node].count(k+2*d-it.ff)) {
ans = min(ans,it.ss+s[node][k+2*d-it.ff]-2*dep);
}
}
for (auto it : s[it.ss]) {
if (s[node].count(it.ff)) s[node][it.ff] = min(s[node][it.ff],it.ss);
else s[node][it.ff] = it.ss;
if (it.ff == k+d) {
ans = min(ans,it.ss-dep);
}
}
s[it.ss].clear();
}
}
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[])
{
k = K;
for (int i=0;i<N-1;i++) {
edges[H[i][0]].push_back({L[i],H[i][1]});
edges[H[i][1]].push_back({L[i],H[i][0]});
}
dfs(0,0);
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... |