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"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef vector<ii> vii;
typedef map<int, int> mii;
const int inf = 1e9;
const int maxn = 2e5+5;
vii adj[maxn];
int dep[maxn];
int dist[maxn];
int k;
int res = inf;
mii dfs(int u, int p) {
mii mp;
mp[dist[u]] = dep[u];
for(auto [v, c] : adj[u]) if(v != p) {
dep[v] = dep[u] + 1;
dist[v] = dist[u] + c;
mii mp2 = dfs(v, u);
if(mp.size() < mp2.size())
swap(mp, mp2);
for(auto [di, de] : mp2) {
auto it = mp.find(k + 2*dist[u] - di);
if(it != mp.end()) ckmin(res, de + it->ss - 2*dep[u]);
}
for(auto [di, de] : mp2) {
auto it = mp.find(di);
if(it != mp.end()) ckmin(it->ss, de);
else mp[di] = de;
}
}
return mp;
}
int best_path(int n, int K, int h[][2], int l[]) {
k = K;
loop(i, n-1) {
int u = h[i][0], v = h[i][1];
int c = l[i];
adj[u].pb({v, c});
adj[v].pb({u, c});
}
dep[0] = 1;
dist[0] = 1;
dfs(0, 0);
return (res == inf ? -1 : res);
}
# | 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... |