This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "race.h"
int n;
ll k;
VVPI gp;
VL dist;
VI depth;
vector<map<ll, int>> st;
void dfs1(int u = 0, int p = -1) {
for (auto[v, w] : gp[u]) if (v != p) {
depth[v] = depth[u]+1;
dist[v] = dist[u] + w;
dfs1(v, u);
}
}
int ans = 2e9;
void dfs2(int u = 0, int p = -1) {
st[u][dist[u]] = depth[u];
for (auto[v, w] : gp[u]) if (v != p) {
dfs2(v, u);
if (st[v].size() > st[u].size()) st[u].swap(st[v]);
for (auto& P : st[v]) {
/*P.ff + x - 2*dist[u] == k;*/
/*x == k + 2*dist[u] - P.ff;*/
ll x = k+2*dist[u]-P.ff;
if (st[u].count(x)) {
/*cout << st[u][x] + P.ss - 2*depth[u] << endl;*/
setmin(ans, st[u][x] + P.ss - 2*depth[u]);
}
}
for (auto& P : st[v]) {
if (!st[u].count(P.ff)) st[u][P.ff] = P.ss;
else setmin(st[u][P.ff], P.ss);
}
}
/*cout << u << ": " << endl;*/
/*for (auto& P : st[u]) {*/
/*cout << " " << P.ff-dist[u] << ", " << P.ss-depth[u] << endl;*/
/*cout << " " << P.ff << ", " << P.ss << endl;*/
/*}*/
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
gp = VVPI(n);
rep(i, n-1) {
int u = H[i][0];
int v = H[i][1];
int w = L[i];
gp[u].pb({v, w});
gp[v].pb({u, w});
}
dist = VL(n);
depth = VI(n);
dfs1();
st = vector<map<ll, int>>(n);
dfs2();
if (ans == 2e9) return -1;
return ans;
}
Compilation message (stderr)
race.cpp: In function 'void dfs1(int, int)':
race.cpp:42:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | for (auto[v, w] : gp[u]) if (v != p) {
| ^
race.cpp: In function 'void dfs2(int, int)':
race.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for (auto[v, w] : gp[u]) if (v != p) {
| ^
# | 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... |