// O O O O O O O O O O O O O O O OO O OO O OO O O O TTCH O TTTCH O TTTCH O O O O
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <random>
#include <iomanip>
#include <bitset>
#include <cassert>
// #include "grader.h"
using namespace std;
#define y1 y11
#define double long double
#define less less228
#define left left228
#define right right228
#define list list228
template<typename T> void uin(T &a, T b) {
if (b < a) a = b;
}
template<typename T> void uax(T &a, T b) {
if (b > a) a = b;
}
const int MAXN = 200 * 1000 + 228;
const int INF = 1e9 + 228;
int n, k;
vector< pair<int, int> > g[MAXN];
int sz[MAXN];
bool blocked[MAXN];
int allsz = 0;
int answer = INF;
void szdfs(int v, int par = -1) {
sz[v] = 1;
++allsz;
for (pair<int, int> go : g[v]) {
if (go.first == par || blocked[go.first]) continue;
szdfs(go.first, v);
sz[v] += sz[go.first];
}
}
int centroid = 0;
void ffc(int v, int par = -1) {
bool goin = 0;
for (pair<int, int> go : g[v]) {
if (go.first == par || blocked[go.first]) continue;
if (sz[go.first] > allsz / 2) {
ffc(go.first, v);
goin = 1;
}
}
if (sz[v] >= allsz / 2 && !goin) centroid = v;
}
int FindCentroid(int v) {
centroid = allsz = 0;
szdfs(v);
ffc(v);
return centroid;
}
int minh[1000 * 1000 + 228];
int h[MAXN];
int w[MAXN];
void godfs(int v, int par, int W) {
w[v] = W;
h[v] = h[par] + 1;
if (w[v] <= k) {
uin(answer, h[v] + minh[k - w[v]]);
}
for (pair<int, int> go : g[v]) {
if (go.first == par || blocked[go.first]) continue;
godfs(go.first, v, W + go.second);
}
}
void update_dfs(int v, int par) {
if (w[v] <= k) {
uin(minh[w[v]], h[v]);
}
for (pair<int, int> go : g[v]) {
if (go.first == par || blocked[go.first]) continue;
update_dfs(go.first, v);
}
}
void update_back(int v, int par) {
if (w[v] <= k) {
minh[w[v]] = INF;
}
for (pair<int, int> go : g[v]) {
if (go.first == par || blocked[go.first]) continue;
update_back(go.first, v);
}
}
void solve(int c) {
minh[0] = 0;
h[c] = w[c] = 0;
for (pair<int, int> go : g[c]) {
if (!blocked[go.first]) {
godfs(go.first, c, go.second);
update_dfs(go.first, c);
}
}
for (pair<int, int> go : g[c]) {
if (!blocked[go.first]) {
update_back(go.first, c);
}
}
}
void build(int v) {
int c = FindCentroid(v);
blocked[c] = 1;
solve(c);
for (pair<int, int> go : g[c]) {
int to = go.first;
if (!blocked[to]) {
build(to);
}
}
}
void dfs(int v, int par = -1, int w = 0, int l = 0) {
if (w == k) uin(answer, l);
for (pair<int, int> go : g[v]) {
int to = go.first, ww = go.second;
if (to == par) continue;
dfs(to, v, w + ww, l + 1);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for (int i = 0; i <= k; ++i) {
minh[i] = INF;
}
for (int i = 0; i < n - 1; ++i) {
++H[i][0], ++H[i][1];
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
answer = INF;
build(1);
if (answer == INF) answer = -1;
return answer;
}
// int H[100][2];
// int L[100];
// signed main() {
// int nn, kk;
// cin >> nn >> kk;
// for (int i = 0; i < nn - 1; ++i) {
// cin >> H[i][0] >> H[i][1] >> L[i];
// }
// cout << best_path(nn, kk, H, L) << '\n';
// }
/*
4 3
0 1 1
1 2 2
1 3 4
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
14 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5016 KB |
Output is correct |
4 |
Correct |
9 ms |
5112 KB |
Output is correct |
5 |
Correct |
10 ms |
5112 KB |
Output is correct |
6 |
Correct |
9 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
5112 KB |
Output is correct |
8 |
Correct |
9 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5112 KB |
Output is correct |
10 |
Correct |
10 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
9 ms |
5112 KB |
Output is correct |
13 |
Correct |
9 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
5112 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
10 ms |
5112 KB |
Output is correct |
18 |
Correct |
10 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
14 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5016 KB |
Output is correct |
4 |
Correct |
9 ms |
5112 KB |
Output is correct |
5 |
Correct |
10 ms |
5112 KB |
Output is correct |
6 |
Correct |
9 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
5112 KB |
Output is correct |
8 |
Correct |
9 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5112 KB |
Output is correct |
10 |
Correct |
10 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
9 ms |
5112 KB |
Output is correct |
13 |
Correct |
9 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
5112 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
10 ms |
5112 KB |
Output is correct |
18 |
Correct |
10 ms |
5112 KB |
Output is correct |
19 |
Correct |
9 ms |
4984 KB |
Output is correct |
20 |
Correct |
9 ms |
5112 KB |
Output is correct |
21 |
Correct |
10 ms |
5116 KB |
Output is correct |
22 |
Correct |
12 ms |
8696 KB |
Output is correct |
23 |
Correct |
12 ms |
8056 KB |
Output is correct |
24 |
Correct |
12 ms |
8440 KB |
Output is correct |
25 |
Correct |
12 ms |
8440 KB |
Output is correct |
26 |
Correct |
11 ms |
6392 KB |
Output is correct |
27 |
Correct |
11 ms |
8184 KB |
Output is correct |
28 |
Correct |
10 ms |
5880 KB |
Output is correct |
29 |
Correct |
10 ms |
6396 KB |
Output is correct |
30 |
Correct |
11 ms |
6648 KB |
Output is correct |
31 |
Correct |
11 ms |
7672 KB |
Output is correct |
32 |
Correct |
11 ms |
7928 KB |
Output is correct |
33 |
Correct |
12 ms |
8184 KB |
Output is correct |
34 |
Correct |
11 ms |
7416 KB |
Output is correct |
35 |
Correct |
11 ms |
8316 KB |
Output is correct |
36 |
Correct |
11 ms |
8696 KB |
Output is correct |
37 |
Correct |
11 ms |
8184 KB |
Output is correct |
38 |
Correct |
11 ms |
7032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
14 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5016 KB |
Output is correct |
4 |
Correct |
9 ms |
5112 KB |
Output is correct |
5 |
Correct |
10 ms |
5112 KB |
Output is correct |
6 |
Correct |
9 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
5112 KB |
Output is correct |
8 |
Correct |
9 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5112 KB |
Output is correct |
10 |
Correct |
10 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
9 ms |
5112 KB |
Output is correct |
13 |
Correct |
9 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
5112 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
10 ms |
5112 KB |
Output is correct |
18 |
Correct |
10 ms |
5112 KB |
Output is correct |
19 |
Correct |
330 ms |
11128 KB |
Output is correct |
20 |
Correct |
306 ms |
12608 KB |
Output is correct |
21 |
Correct |
324 ms |
12536 KB |
Output is correct |
22 |
Correct |
202 ms |
12664 KB |
Output is correct |
23 |
Correct |
152 ms |
13048 KB |
Output is correct |
24 |
Correct |
107 ms |
12792 KB |
Output is correct |
25 |
Correct |
277 ms |
14072 KB |
Output is correct |
26 |
Correct |
121 ms |
15864 KB |
Output is correct |
27 |
Correct |
322 ms |
20472 KB |
Output is correct |
28 |
Correct |
786 ms |
27256 KB |
Output is correct |
29 |
Correct |
809 ms |
26616 KB |
Output is correct |
30 |
Correct |
306 ms |
20344 KB |
Output is correct |
31 |
Correct |
321 ms |
20648 KB |
Output is correct |
32 |
Correct |
450 ms |
20472 KB |
Output is correct |
33 |
Correct |
512 ms |
19192 KB |
Output is correct |
34 |
Correct |
525 ms |
20088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
14 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5016 KB |
Output is correct |
4 |
Correct |
9 ms |
5112 KB |
Output is correct |
5 |
Correct |
10 ms |
5112 KB |
Output is correct |
6 |
Correct |
9 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
5112 KB |
Output is correct |
8 |
Correct |
9 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5112 KB |
Output is correct |
10 |
Correct |
10 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
9 ms |
5112 KB |
Output is correct |
13 |
Correct |
9 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
5112 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
10 ms |
5112 KB |
Output is correct |
18 |
Correct |
10 ms |
5112 KB |
Output is correct |
19 |
Correct |
9 ms |
4984 KB |
Output is correct |
20 |
Correct |
9 ms |
5112 KB |
Output is correct |
21 |
Correct |
10 ms |
5116 KB |
Output is correct |
22 |
Correct |
12 ms |
8696 KB |
Output is correct |
23 |
Correct |
12 ms |
8056 KB |
Output is correct |
24 |
Correct |
12 ms |
8440 KB |
Output is correct |
25 |
Correct |
12 ms |
8440 KB |
Output is correct |
26 |
Correct |
11 ms |
6392 KB |
Output is correct |
27 |
Correct |
11 ms |
8184 KB |
Output is correct |
28 |
Correct |
10 ms |
5880 KB |
Output is correct |
29 |
Correct |
10 ms |
6396 KB |
Output is correct |
30 |
Correct |
11 ms |
6648 KB |
Output is correct |
31 |
Correct |
11 ms |
7672 KB |
Output is correct |
32 |
Correct |
11 ms |
7928 KB |
Output is correct |
33 |
Correct |
12 ms |
8184 KB |
Output is correct |
34 |
Correct |
11 ms |
7416 KB |
Output is correct |
35 |
Correct |
11 ms |
8316 KB |
Output is correct |
36 |
Correct |
11 ms |
8696 KB |
Output is correct |
37 |
Correct |
11 ms |
8184 KB |
Output is correct |
38 |
Correct |
11 ms |
7032 KB |
Output is correct |
39 |
Correct |
330 ms |
11128 KB |
Output is correct |
40 |
Correct |
306 ms |
12608 KB |
Output is correct |
41 |
Correct |
324 ms |
12536 KB |
Output is correct |
42 |
Correct |
202 ms |
12664 KB |
Output is correct |
43 |
Correct |
152 ms |
13048 KB |
Output is correct |
44 |
Correct |
107 ms |
12792 KB |
Output is correct |
45 |
Correct |
277 ms |
14072 KB |
Output is correct |
46 |
Correct |
121 ms |
15864 KB |
Output is correct |
47 |
Correct |
322 ms |
20472 KB |
Output is correct |
48 |
Correct |
786 ms |
27256 KB |
Output is correct |
49 |
Correct |
809 ms |
26616 KB |
Output is correct |
50 |
Correct |
306 ms |
20344 KB |
Output is correct |
51 |
Correct |
321 ms |
20648 KB |
Output is correct |
52 |
Correct |
450 ms |
20472 KB |
Output is correct |
53 |
Correct |
512 ms |
19192 KB |
Output is correct |
54 |
Correct |
525 ms |
20088 KB |
Output is correct |
55 |
Correct |
20 ms |
5880 KB |
Output is correct |
56 |
Correct |
21 ms |
5752 KB |
Output is correct |
57 |
Correct |
106 ms |
12792 KB |
Output is correct |
58 |
Correct |
52 ms |
12568 KB |
Output is correct |
59 |
Correct |
126 ms |
16632 KB |
Output is correct |
60 |
Correct |
882 ms |
30628 KB |
Output is correct |
61 |
Correct |
311 ms |
20596 KB |
Output is correct |
62 |
Correct |
320 ms |
24312 KB |
Output is correct |
63 |
Correct |
469 ms |
24464 KB |
Output is correct |
64 |
Correct |
980 ms |
22776 KB |
Output is correct |
65 |
Correct |
606 ms |
21392 KB |
Output is correct |
66 |
Correct |
854 ms |
29432 KB |
Output is correct |
67 |
Correct |
182 ms |
25188 KB |
Output is correct |
68 |
Correct |
485 ms |
25208 KB |
Output is correct |
69 |
Correct |
622 ms |
25208 KB |
Output is correct |
70 |
Correct |
753 ms |
24696 KB |
Output is correct |