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"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/* DEFINES */
#define S second
#define F first
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define pp(a, b) pair<a, b>
#define pq(a) priority_queue<a>
#define qq(a) queue<a>
#define ss(a) set<a>
#define mm(a, b) map<a, b>
#define ump(a, b) unordered_map<a, b>
#define sync \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define elif else if
#define endl "\n"
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define pb push_back
#define logi(a) __lg(a)
#define sqrt(a) sqrtl(a)
#define mpr make_pair
#define ins insert
using namespace std;
using namespace __gnu_pbds;
using namespace __cxx11;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 2e5 + 5;
vv(pp(ll, ll)) g[N];
ll sub[N], used[N], depth[N], depth1[N], iscentroid[N];
void dfs(ll v)
{
used[v] = 1;
sub[v] = 1;
for (auto [to, w] : g[v])
if (!used[to] and !iscentroid[to])
dfs(to), sub[v] += sub[to];
}
void dfs1(ll v, multiset<pp(ll, ll)> &s, vv(ll) & child)
{
used[v] = 1;
for (auto [to, w] : g[v])
if (!used[to] and !iscentroid[to])
depth[to] = depth[v] + 1, depth1[to] = depth[v] + w, dfs1(to, s, child);
s.ins(mpr(depth1[v], depth[v]));
child.pb(v);
}
ll getcentroid(ll v, ll sz)
{
used[v] = 1;
for (auto [to, w] : g[v])
if (!used[to] and sub[to] > (sz >> 1))
return getcentroid(to, sz);
return v;
}
ll findcentroid(ll v)
{
memset(used, 0, sizeof(used));
dfs(v);
memset(used, 0, sizeof(used));
return getcentroid(v, sub[v]);
}
ll solve(ll v, ll k)
{
ll ans = INF;
v = findcentroid(v);
iscentroid[v] = true;
depth[v] = depth1[v] = 0;
multiset<pp(ll, ll)> s;
vv(ll) child;
memset(used, 0, sizeof(used));
dfs1(v, s, child);
// cout << v << " ";
// for (ll i : child)
// cout << i << " ";
// cout << endl;
child.pop_back();
if (child.empty())
return INF;
ll i = 0, last = 0;
multiset<pp(ll, ll)>::iterator it;
for (auto [to, w] : g[v])
{
if (iscentroid[to])
continue;
while (child[i] != to)
s.erase(s.find(mpr(depth1[child[i]], depth[child[i]]))), i++;
s.erase(s.find(mpr(depth1[to], depth[to])));
if (!s.empty())
{
i = last;
while (child[i] != to)
{
it = s.lower_bound(mpr(k - depth1[child[i]], -100));
if (it != s.end() and it->F + depth1[child[i]] == k)
ans = min(ans, it->S + depth[child[i]]);
i++;
}
it = s.lower_bound(mpr(k - depth1[to], -100));
if (it != s.end() and it->F + depth1[to] == k)
ans = min(ans, it->S + depth[to]);
}
i = last;
while (child[i] != to)
s.ins(mpr(depth1[child[i]], depth[child[i]])), i++;
s.ins(mpr(depth1[to], depth[to]));
i++;
last = i;
}
for (auto [to, w] : g[v])
if (!iscentroid[to])
ans = min(ans, solve(to, k));
return ans;
}
int best_path(int n, int k, int e[][2], int w[])
{
for (ll i = 0; i < n - 1; i++)
g[e[i][0]].pb(mpr(e[i][1], w[i])), g[e[i][1]].pb(mpr(e[i][0], w[i]));
ll ans = solve(0, k);
return ans == INF ? -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... |