#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <chrono>
#include <set>
#include <map>
#include <stack>
#include <functional>
#include <iomanip>
#include <queue>
#include <cassert>
#include <complex>
#include <cstring>
#include <memory>
#include <bitset>
#include <sstream>
#include <cmath>
#include <numeric>
#include <numbers>
#include <fstream>
#include "race.h"
using namespace std;
#ifndef template
#ifndef define
#define ll long long
#define ld long double
#define pl pair<ll, ll>
#define pi pair<int, int>
#define nl cout << '\n';
#define x first
#define y second
#define cbit(x) __builtin_popcountll(x)
#define uid(a, b) uniform_int_distribution<ll>(a, b)(rng)
#define siz(x) (int)x.size()
#endif
#ifndef print
void print(size_t x) {cout << x << ' ';}
void print(int x) {cout << x << ' ';}
void print(long long x) {cout << x << ' ';}
void print(float x) {cout << x << ' ';}
void print(long double x) {cout << x << ' ';}
void print(char x) {cout << x << ' ';}
void print(const char* x) {cout << x << ' ';}
void print(bool x) {cout << x << ' ';}
void print(string &x) {cout << x << ' ';}
template<typename T, typename V>
void print(pair<T, V> &p) {print(p.x); print(p.y);}
template<typename T>
void print(vector<T> v) {for (int i = 0; i < v.size(); i++) print(v[i]);}
template<typename T>
void print(vector<vector<T>> v) {
for (int i = 0; i < v.size(); i++){
for (int j = 0; j < v[i].size(); j++)
print(v[i][j]);
nl;
}
}
template <typename T, typename... V>
void print(T t, V&&... v) {print(t); print(v...);}
#endif
#ifndef read
void read(int &x) {cin >> x;}
void read(long long &x) {cin >> x;}
void read(unsigned &x) {cin >> x;}
void read(unsigned long long &x) {cin >> x;}
void read(float &x) {cin >> x;}
void read(long double &x) {cin >> x;}
void read(char &x) {cin >> x;}
void read(string &x) {cin >> x;}
void read(bool &x) {cin >> x;}
template<typename T>
void read(vector<T> &v) {
for (int i = 0; i < v.size(); i++)
read(v[i]);
}
template <typename T, typename... V>
void read(T &t, V&... v) {read(t); read(v...);}
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> bool maxi(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template<class T> bool mini(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
template<class... Args>
auto vec(size_t n, Args&&... args) {
if constexpr(sizeof...(args) == 1)
return vector(n, args...);
else
return vector(n, vec(args...));
}
#endif
using namespace std;
const ll inf = 1e15;
const ll def = 2e5+1;
const ll mod = 998244353;
set<pl> s[def];
vector<pl> edg[def];
ll h[def], wi[def];
ll res = inf;
void dfs(int u, int pre, ll k, ll crr, ll height){
s[u].insert({wi[u], h[u]});
for (auto [v, w] : edg[u]){
if (v == pre)
continue;
h[v] = h[u] + 1;
wi[v] = wi[u] + w;
dfs(v, u, k, crr + w, height + 1);
if (s[v].size() > s[u].size())
s[u].swap(s[v]);
for (auto [wv, len] : s[v]){
ll need = k + 2 * crr - wv;
auto it = s[u].lower_bound({need, -inf});
if ((*it).x == need)
mini(res, (*it).y + len - 2 * height);
}
for (auto [wv, len] : s[v]){
auto it = s[u].lower_bound({wv, -inf});
if ((*it).x == wv && len < (*it).y){
s[u].erase(*it);
s[u].insert({wv, len});
}
else
s[u].insert({wv, len});
}
}
ll need = k + crr;
auto it = s[u].lower_bound({need, -inf});
if ((*it).x == need)
mini(res, (*it).y - crr);
}
/*
wi_u + wi_v - 2 * crr = k
wi_v = k + 2 * crr - wi_u
*/
int best_path(int N, int K, int H[][2], int L[]){
for (int i = 0; i < N - 1; i++){
int u = H[i][0], v = H[i][1], w = L[i];
edg[u].push_back({v, w});
edg[v].push_back({u, w});
}
dfs(0, -1, K, 0, 0);
if (res == inf)
return -1;
else
return 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... |