/*
Author: Cadocx
Codeforces: https://codeforces.com/profile/Kadoc
VNOJ: oj.vnoi.info/user/Cadoc
*/
#include <bits/stdc++.h>
using namespace std;
// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return 1; }; return 0; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return 1; }; return 0; }
//STL
#define sz(x) (int)(x).size()
#define FOR(i,l,r) for(auto i = l; i <= r; i++)
#define FORD(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define maxN 200005
#define MOD 1000230007
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0
int n, k;
int H[maxN][2], L[maxN];
int pos[maxN], p[maxN << 2][22], Timer = 0;
int h[maxN], d[maxN];
int sz[maxN], del[maxN];
vector<pii> g[maxN];
int f[1000005];
vector<int> V;
int Ans = INF;
void dfs(int u, int pr = -1){
pos[u] = ++Timer;
p[Timer][0] = u;
for(auto [v, w]:g[u]) if(v != pr){
h[v] = h[u] + 1;
d[v] = d[u] + w;
dfs(v, u);
p[++Timer][0] = u;
}
}
void dfs_sz(int u, int p = -1){
sz[u] = 1;
for(auto [v, w]:g[u]) if(v != p && !del[v]){
dfs_sz(v, u);
sz[u] += sz[v];
}
}
int getMin(int u, int v){
return (h[u] < h[v]? u:v);
}
int lca(int u, int v){
int l = pos[u], r = pos[v];
if(l > r) swap(l, r);
int k = 31 - __builtin_clz(r-l+1);
return getMin(p[l][k], p[r-mask(k)+1][k]);
}
int dist(int u, int v){
return h[u] + h[v] - 2 * h[lca(u, v)];
}
int distW(int u, int v){
return d[u] + d[v] - 2 * d[lca(u, v)];
}
void upd(int u, int root, int p = -1){
int hu = dist(u, root), du = distW(u, root);
if(k >= du){
minimize(f[du], hu);
V.pb(du);
}
for(auto [v, w]:g[u]) if(v != p && !del[v]) upd(v, root, u);
}
void get(int u, int root, int p = -1){
int hu = dist(u, root), du = distW(u, root);
if(k >= du) minimize(Ans, hu + f[k - du]);
for(auto [v, w]:g[u]) if(v != p && !del[v]) get(v, root, u);
}
int centroid(int u, int p, int n){
for(auto [v, w]:g[u]) if(v != p && !del[v] && sz[v] > n/2) return centroid(v, u, n);
return u;
}
void cd(int u, int p = -1){
dfs_sz(u);
u = centroid(u, p, sz[u]);
del[u] = 1;
f[0] = 0;
for(auto [v, w]:g[u]) if(!del[v]){
get(v, u);
upd(v, u);
}
for(int depth:V) f[depth] = INF;
V.clear();
for(auto [v, w]:g[u]) if(!del[v]) cd(v, u);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
FOR(i, 0, n-2){
int u = H[i][0], v = H[i][1], w = L[i];
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(1);
for(int j=1; mask(j)<=Timer; ++j) for(int i=1; i+mask(j)-1<=Timer; ++i){
p[i][j] = getMin(p[i][j-1], p[i+mask(j-1)][j-1]);
}
memset(f, 0x3f, sizeof f);
cd(1);
if(Ans >= INF) Ans = -1;
return Ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
3 ms |
9052 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
3 ms |
9052 KB |
Output is correct |
6 |
Correct |
3 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
8900 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
3 ms |
9000 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
3 ms |
9052 KB |
Output is correct |
12 |
Correct |
3 ms |
9116 KB |
Output is correct |
13 |
Correct |
3 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9304 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
3 ms |
9052 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
3 ms |
9052 KB |
Output is correct |
6 |
Correct |
3 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
8900 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
3 ms |
9000 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
3 ms |
9052 KB |
Output is correct |
12 |
Correct |
3 ms |
9116 KB |
Output is correct |
13 |
Correct |
3 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9304 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9048 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
21 |
Correct |
4 ms |
9308 KB |
Output is correct |
22 |
Correct |
4 ms |
9308 KB |
Output is correct |
23 |
Correct |
4 ms |
9308 KB |
Output is correct |
24 |
Correct |
4 ms |
9308 KB |
Output is correct |
25 |
Correct |
4 ms |
9308 KB |
Output is correct |
26 |
Correct |
4 ms |
9320 KB |
Output is correct |
27 |
Correct |
4 ms |
9308 KB |
Output is correct |
28 |
Correct |
4 ms |
9264 KB |
Output is correct |
29 |
Correct |
4 ms |
9308 KB |
Output is correct |
30 |
Correct |
4 ms |
9224 KB |
Output is correct |
31 |
Correct |
5 ms |
9308 KB |
Output is correct |
32 |
Correct |
4 ms |
9308 KB |
Output is correct |
33 |
Correct |
4 ms |
9308 KB |
Output is correct |
34 |
Correct |
6 ms |
9308 KB |
Output is correct |
35 |
Correct |
4 ms |
9112 KB |
Output is correct |
36 |
Correct |
4 ms |
9304 KB |
Output is correct |
37 |
Correct |
4 ms |
9308 KB |
Output is correct |
38 |
Correct |
4 ms |
9308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
3 ms |
9052 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
3 ms |
9052 KB |
Output is correct |
6 |
Correct |
3 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
8900 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
3 ms |
9000 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
3 ms |
9052 KB |
Output is correct |
12 |
Correct |
3 ms |
9116 KB |
Output is correct |
13 |
Correct |
3 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9304 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9048 KB |
Output is correct |
19 |
Correct |
134 ms |
34376 KB |
Output is correct |
20 |
Correct |
146 ms |
34388 KB |
Output is correct |
21 |
Correct |
130 ms |
34516 KB |
Output is correct |
22 |
Correct |
114 ms |
34764 KB |
Output is correct |
23 |
Correct |
120 ms |
34640 KB |
Output is correct |
24 |
Correct |
70 ms |
34644 KB |
Output is correct |
25 |
Correct |
157 ms |
40788 KB |
Output is correct |
26 |
Correct |
91 ms |
42196 KB |
Output is correct |
27 |
Correct |
174 ms |
59940 KB |
Output is correct |
28 |
Correct |
390 ms |
73416 KB |
Output is correct |
29 |
Correct |
400 ms |
68436 KB |
Output is correct |
30 |
Correct |
171 ms |
59860 KB |
Output is correct |
31 |
Correct |
188 ms |
59964 KB |
Output is correct |
32 |
Correct |
235 ms |
59984 KB |
Output is correct |
33 |
Correct |
290 ms |
58704 KB |
Output is correct |
34 |
Correct |
309 ms |
59472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
3 ms |
9052 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
3 ms |
9052 KB |
Output is correct |
6 |
Correct |
3 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
8900 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
3 ms |
9000 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
3 ms |
9052 KB |
Output is correct |
12 |
Correct |
3 ms |
9116 KB |
Output is correct |
13 |
Correct |
3 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9304 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9048 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
3 ms |
9052 KB |
Output is correct |
21 |
Correct |
4 ms |
9308 KB |
Output is correct |
22 |
Correct |
4 ms |
9308 KB |
Output is correct |
23 |
Correct |
4 ms |
9308 KB |
Output is correct |
24 |
Correct |
4 ms |
9308 KB |
Output is correct |
25 |
Correct |
4 ms |
9308 KB |
Output is correct |
26 |
Correct |
4 ms |
9320 KB |
Output is correct |
27 |
Correct |
4 ms |
9308 KB |
Output is correct |
28 |
Correct |
4 ms |
9264 KB |
Output is correct |
29 |
Correct |
4 ms |
9308 KB |
Output is correct |
30 |
Correct |
4 ms |
9224 KB |
Output is correct |
31 |
Correct |
5 ms |
9308 KB |
Output is correct |
32 |
Correct |
4 ms |
9308 KB |
Output is correct |
33 |
Correct |
4 ms |
9308 KB |
Output is correct |
34 |
Correct |
6 ms |
9308 KB |
Output is correct |
35 |
Correct |
4 ms |
9112 KB |
Output is correct |
36 |
Correct |
4 ms |
9304 KB |
Output is correct |
37 |
Correct |
4 ms |
9308 KB |
Output is correct |
38 |
Correct |
4 ms |
9308 KB |
Output is correct |
39 |
Correct |
134 ms |
34376 KB |
Output is correct |
40 |
Correct |
146 ms |
34388 KB |
Output is correct |
41 |
Correct |
130 ms |
34516 KB |
Output is correct |
42 |
Correct |
114 ms |
34764 KB |
Output is correct |
43 |
Correct |
120 ms |
34640 KB |
Output is correct |
44 |
Correct |
70 ms |
34644 KB |
Output is correct |
45 |
Correct |
157 ms |
40788 KB |
Output is correct |
46 |
Correct |
91 ms |
42196 KB |
Output is correct |
47 |
Correct |
174 ms |
59940 KB |
Output is correct |
48 |
Correct |
390 ms |
73416 KB |
Output is correct |
49 |
Correct |
400 ms |
68436 KB |
Output is correct |
50 |
Correct |
171 ms |
59860 KB |
Output is correct |
51 |
Correct |
188 ms |
59964 KB |
Output is correct |
52 |
Correct |
235 ms |
59984 KB |
Output is correct |
53 |
Correct |
290 ms |
58704 KB |
Output is correct |
54 |
Correct |
309 ms |
59472 KB |
Output is correct |
55 |
Correct |
12 ms |
11612 KB |
Output is correct |
56 |
Correct |
12 ms |
11676 KB |
Output is correct |
57 |
Correct |
86 ms |
35152 KB |
Output is correct |
58 |
Correct |
50 ms |
35272 KB |
Output is correct |
59 |
Correct |
97 ms |
42188 KB |
Output is correct |
60 |
Correct |
441 ms |
67780 KB |
Output is correct |
61 |
Correct |
204 ms |
60372 KB |
Output is correct |
62 |
Correct |
217 ms |
60716 KB |
Output is correct |
63 |
Correct |
257 ms |
60876 KB |
Output is correct |
64 |
Correct |
422 ms |
61288 KB |
Output is correct |
65 |
Correct |
323 ms |
61096 KB |
Output is correct |
66 |
Correct |
420 ms |
72272 KB |
Output is correct |
67 |
Correct |
107 ms |
62656 KB |
Output is correct |
68 |
Correct |
248 ms |
60692 KB |
Output is correct |
69 |
Correct |
234 ms |
61328 KB |
Output is correct |
70 |
Correct |
226 ms |
58324 KB |
Output is correct |