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;
#ifdef LOCAL
#include <debug.h>
#include "grader.cpp"
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Practising for VOI23 gold medal
*/
const int oo = 1e9;
const int MAXN = 2e5 + 1;
const int MAXK = 1e6 + 1;
int n, k, h[MAXN], h1[MAXN], s[MAXN], c[MAXN], rs = oo;
vi vt;
vpi adj[MAXN];
multiset<int> hset[MAXK];
void dfs(int u, int p) {
s[u] = 1;
EACH(j, adj[u]) {
int v = j.st, w = j.nd;
if (c[v] || v == p) continue;
h[v] = h[u] + w;
h[v] = min(h[v], k + 1);
h1[v] = h1[u] + 1;
dfs(v, u);
s[u] += s[v];
}
}
void dfs1(int u, int p) {
vt.pb(u);
EACH(j, adj[u]) {
int v = j.st;
if (c[v] || v == p) continue;
dfs1(v, u);
}
}
int findct(int u, int p, int total) {
EACH(j, adj[u]) {
int v = j.st;
if (c[v] || v == p) continue;
if (s[v] > total / 2) return findct(v, u, total);
}
return u;
}
void centroid(int u) {
h[u] = h1[u] = 0;
dfs(u, 0);
int ct = findct(u, 0, s[u]);
h[ct] = h1[ct] = 0;
dfs(ct, 0);
c[ct] = 1;
EACH(j, adj[ct]) {
int v = j.st;
if (c[v]) continue;
dfs1(v, ct);
EACH(z, vt) {
int hn = h[z];
if (hn == k) rs = min(rs, h1[z]);
if (hn > k) continue;
hset[hn].insert(h1[z]);
}
vt.clear();
}
EACH(j, adj[ct]) {
int v = j.st;
if (c[v]) continue;
dfs1(v, ct);
EACH(z, vt) {
int hn = h[z];
if (hn > k) continue;
hset[hn].erase(hset[hn].lb(h1[z]));
}
EACH(z, vt) {
int hn = h[z];
if (hn > k) continue;
int tmp = k - hn;
if (!hset[tmp].empty()) rs = min(rs, h1[z] + *hset[tmp].begin());
}
EACH(z, vt) {
int hn = h[z];
if (hn > k) continue;
hset[hn].insert(h1[z]);
}
vt.clear();
}
EACH(j, adj[ct]) {
int v = j.st;
if (c[v]) continue;
dfs1(v, ct);
EACH(z, vt) {
int hn = h[z];
if (hn > k) continue;
hset[hn].erase(hset[hn].lb(h1[z]));
}
vt.clear();
}
EACH(j, adj[ct]) {
int v = j.st;
if (c[v]) continue;
centroid(v);
}
}
int best_path(int n, int k, int h[][2], int l[]) {
::k = k;
FOR(i, 0, n - 2) {
int u = h[i][0] + 1, v = h[i][1] + 1, w = l[i];
adj[u].pb({v, w});
adj[v].pb({u, w});
}
centroid(1);
return rs < oo? rs : -1;
}
/*
3 1
1 0 3
2 1 1
1
*/
Compilation message (stderr)
race.cpp: In function 'void dfs(int, int)':
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:47:2: note: in expansion of macro 'EACH'
47 | EACH(j, adj[u]) {
| ^~~~
race.cpp: In function 'void dfs1(int, int)':
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:59:2: note: in expansion of macro 'EACH'
59 | EACH(j, adj[u]) {
| ^~~~
race.cpp: In function 'int findct(int, int, int)':
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:66:2: note: in expansion of macro 'EACH'
66 | EACH(j, adj[u]) {
| ^~~~
race.cpp: In function 'void centroid(int)':
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:80:2: note: in expansion of macro 'EACH'
80 | EACH(j, adj[ct]) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:84:3: note: in expansion of macro 'EACH'
84 | EACH(z, vt) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:92:2: note: in expansion of macro 'EACH'
92 | EACH(j, adj[ct]) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:96:3: note: in expansion of macro 'EACH'
96 | EACH(z, vt) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:101:3: note: in expansion of macro 'EACH'
101 | EACH(z, vt) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:107:3: note: in expansion of macro 'EACH'
107 | EACH(z, vt) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:114:2: note: in expansion of macro 'EACH'
114 | EACH(j, adj[ct]) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:118:3: note: in expansion of macro 'EACH'
118 | EACH(z, vt) {
| ^~~~
race.cpp:28:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
28 | #define EACH(i, x) for (auto &(i) : (x))
| ^
race.cpp:125:2: note: in expansion of macro 'EACH'
125 | EACH(j, adj[ct]) {
| ^~~~
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
race.cpp:133:2: note: in expansion of macro 'FOR'
133 | FOR(i, 0, n - 2) {
| ^~~
# | 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... |