This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#include "race.h"
#endif
const int N = 200'000 + 1;
vector<pair<int, int>> ad[N];
namespace sub2 {
long long d[N];
int dep[N];
int f[N][10], st[N], ed[N], it;
void dfs(int u, int pre = 0) {
st[u] = ++it;
for (int i = 1; i <= 9; ++i)
f[u][i] = f[f[u][i - 1]][i - 1];
for (const auto& [v, w] : ad[u]) {
if (v == pre) continue;
d[v] = d[u] + w;
dep[v] = dep[u] + 1;
f[v][0] = u;
dfs(v, u);
}
ed[u] = it;
}
bool anc(int u, int v) { return st[u] <= st[v] && ed[u] >= ed[v]; }
int lca(int u, int v) {
if (anc(u, v)) return u;
if (anc(v, u)) return v;
for (int i = 9; i >= 0; --i)
if (!anc(f[u][i], v)) u = f[u][i];
return f[u][0];
}
int solve(int n, int k) {
dfs(0);
int answer = 1e7;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int l = lca(i, j);
int distant = d[i] + d[j] - 2 * d[l];
if (distant == k) answer = min(answer, dep[i] + dep[j] - 2 * dep[l]);
}
}
return answer == 1e7 ? -1 : answer;
}
}
namespace sub3 {
int dpin[N][101];
int n, k;
void dfs1(int u, int pre = 0) {
dpin[u][0] = 0;
for (const auto& [v, w] : ad[u]) {
if (v == pre) continue;
dfs1(v, u);
for (int i = 0; i <= k; ++i) if (i - w >= 0) dpin[u][i] = min(dpin[u][i], dpin[v][i - w] + 1);
}
}
int answer;
void dfs2(int u, int pre = 0) {
return;
if (!u) answer = 1e7;
vector<priority_queue<pair<int, int>>> best(k + 1);
for (const auto&[v, w] : ad[u]) {
if (v == pre) continue;
for (int i = 0; i <= k; ++i) if (i - w >= 0) {
best[i].push({dpin[v][i - w], v});
if (best[i].size() > 2) best[i].pop();
}
}
for (int i = 0; i <= k; ++i) {
best[i].push({1e7, -1});
if (best[i].size() > 2) best[i].pop();
best[i].push({1e7, -1});
if (best[i].size() > 2) best[i].pop();
}
for (const auto&[v, w] : ad[u]) {
if (v == pre || k - w < 0) continue;
for (int i = 0; i <= k; ++i) {
int preW = k - i - w;
if (preW < 0) continue;
auto top = best[preW].top(); best[preW].pop();
int val = dpin[v][i] + (best[preW].top().second == v ? top.first : best[preW].top().first) + 2;
answer = min(answer, val);
best[preW].push(top);
}
best.clear();
dfs2(v, u);
}
}
int solve(int a, int b) {
n = a; k = b;
memset(dpin, 63, sizeof dpin);
dfs1(0); dfs2(0);
for (int i = 0; i <= n; ++i) answer = min(answer, dpin[i][k]);
return (answer > n ? -1 : answer);
}
}
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];
if (u > v) swap(u, v);
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
int sub = (n <= 1000 ? 2 : k <= 100 ? 3 : 4);
if (sub == 2) return sub2::solve(n, k);
if (sub == 3) return sub3::solve(n, k);
return -1;
}
#ifdef LOCAL
#define MAX_AA 500000
static int AA, K;
static int H[MAX_AA][2];
static int L[MAX_AA];
static int solution;
#define int long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return l + rng() % (r - l + 1); }
inline
void my_assert(int e) {if (!e) abort();}
void read_input()
{
AA = rnd(2e5, 2e5), K = rnd(100, 100);
// cin >> AA >> K;
for (int i = 0; i < AA - 1; ++i) {
H[i][0] = rnd(i, i);
H[i][1] = i + 1;
L[i] = rnd(1, 100);
// cin >> H[i][0] >> H[i][1] >> L[i];
}
solution = -1;
// cin >> solution;
}
int32_t main() {
int ans;
read_input();
ans = best_path(AA,K,H,L);
cout << ans << "\n";
return 0;
}
#endif
# | 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... |