#include <iostream>
#include <vector>
#include <stack>
using namespace std;
bool remov[200005];
int sz[200005];
int Z;
int res = 1000000000;
vector<pair<int, int>> g[200005];
stack<pair<int, int>> s1;
stack<int> s2;
int su[1000005];
int centroid(int x, int tam, int p=-1) {
for(pair<int, int> y : g[x]) {
if(y.first == p || remov[y.first]) continue;
if(sz[y.first]*2 > tam) return centroid(y.first, tam, x);
}
return x;
}
int subtree(int x, int p=-1) {
sz[x] = 1;
for(pair<int, int> y : g[x]) {
if(y.first == p || remov[y.first]) continue;
sz[x] += subtree(y.first, x);
}
return sz[x];
}
void dfs(int x, int p, int dist, int cont) {
if(dist > Z) return;
if(dist == Z) {
res = min(res, cont);
//cout << x << " " << cont << endl;
return;
}
if(su[Z-dist] > 0) {
res = min(res, su[Z-dist]+cont);
}
//su[dist] = min(cont, su[dist]);
s1.push({dist, cont});
s2.push(dist);
for(pair<int, int> y : g[x]) {
if(y.first == p || remov[y.first]) continue;
dfs(y.first, x, dist+y.second, cont+1);
}
}
int xd;
pair<int, int> lol;
void centdecomp(int x) {
int cent = centroid(x, subtree(x));
for(pair<int, int> y : g[cent]) {
if(y.first == cent || remov[y.first]) continue;
while(!s1.empty()) {
lol = s1.top();
s1.pop();
if(su[lol.first] != 0) {
su[lol.first] = min(su[lol.first], lol.second);
}
else {
su[lol.first] = lol.second;
}
}
dfs(y.first, cent, y.second, 1);
}
while(!s2.empty()) {
xd = s2.top();
s2.pop();
su[xd] = 0;
}
while(!s1.empty()) {
s1.pop();
}
remov[cent] = true;
for(pair<int, int> y : g[cent]) {
if(remov[y.first]) continue;
centdecomp(y.first);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
for(int i=0; i<N-1; i++) {
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
Z = K;
centdecomp(0);
if(res == 1000000000) return -1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10596 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10632 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10584 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10596 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10632 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10584 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
12124 KB |
Output is correct |
23 |
Correct |
4 ms |
12124 KB |
Output is correct |
24 |
Correct |
3 ms |
12380 KB |
Output is correct |
25 |
Correct |
4 ms |
11868 KB |
Output is correct |
26 |
Correct |
3 ms |
11612 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
11356 KB |
Output is correct |
30 |
Correct |
3 ms |
11720 KB |
Output is correct |
31 |
Correct |
3 ms |
12124 KB |
Output is correct |
32 |
Correct |
3 ms |
12124 KB |
Output is correct |
33 |
Correct |
4 ms |
12632 KB |
Output is correct |
34 |
Correct |
3 ms |
12380 KB |
Output is correct |
35 |
Correct |
3 ms |
12636 KB |
Output is correct |
36 |
Correct |
3 ms |
12636 KB |
Output is correct |
37 |
Correct |
3 ms |
12120 KB |
Output is correct |
38 |
Correct |
3 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10596 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10632 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10584 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
72 ms |
17236 KB |
Output is correct |
20 |
Correct |
69 ms |
17988 KB |
Output is correct |
21 |
Correct |
82 ms |
18192 KB |
Output is correct |
22 |
Correct |
72 ms |
18768 KB |
Output is correct |
23 |
Correct |
49 ms |
18692 KB |
Output is correct |
24 |
Correct |
38 ms |
18460 KB |
Output is correct |
25 |
Correct |
77 ms |
20988 KB |
Output is correct |
26 |
Correct |
63 ms |
24596 KB |
Output is correct |
27 |
Correct |
100 ms |
22908 KB |
Output is correct |
28 |
Correct |
153 ms |
34128 KB |
Output is correct |
29 |
Correct |
169 ms |
33680 KB |
Output is correct |
30 |
Correct |
97 ms |
23536 KB |
Output is correct |
31 |
Correct |
94 ms |
23380 KB |
Output is correct |
32 |
Correct |
129 ms |
23244 KB |
Output is correct |
33 |
Correct |
127 ms |
22288 KB |
Output is correct |
34 |
Correct |
107 ms |
22868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10596 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10632 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10584 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
12124 KB |
Output is correct |
23 |
Correct |
4 ms |
12124 KB |
Output is correct |
24 |
Correct |
3 ms |
12380 KB |
Output is correct |
25 |
Correct |
4 ms |
11868 KB |
Output is correct |
26 |
Correct |
3 ms |
11612 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
3 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
11356 KB |
Output is correct |
30 |
Correct |
3 ms |
11720 KB |
Output is correct |
31 |
Correct |
3 ms |
12124 KB |
Output is correct |
32 |
Correct |
3 ms |
12124 KB |
Output is correct |
33 |
Correct |
4 ms |
12632 KB |
Output is correct |
34 |
Correct |
3 ms |
12380 KB |
Output is correct |
35 |
Correct |
3 ms |
12636 KB |
Output is correct |
36 |
Correct |
3 ms |
12636 KB |
Output is correct |
37 |
Correct |
3 ms |
12120 KB |
Output is correct |
38 |
Correct |
3 ms |
11352 KB |
Output is correct |
39 |
Correct |
72 ms |
17236 KB |
Output is correct |
40 |
Correct |
69 ms |
17988 KB |
Output is correct |
41 |
Correct |
82 ms |
18192 KB |
Output is correct |
42 |
Correct |
72 ms |
18768 KB |
Output is correct |
43 |
Correct |
49 ms |
18692 KB |
Output is correct |
44 |
Correct |
38 ms |
18460 KB |
Output is correct |
45 |
Correct |
77 ms |
20988 KB |
Output is correct |
46 |
Correct |
63 ms |
24596 KB |
Output is correct |
47 |
Correct |
100 ms |
22908 KB |
Output is correct |
48 |
Correct |
153 ms |
34128 KB |
Output is correct |
49 |
Correct |
169 ms |
33680 KB |
Output is correct |
50 |
Correct |
97 ms |
23536 KB |
Output is correct |
51 |
Correct |
94 ms |
23380 KB |
Output is correct |
52 |
Correct |
129 ms |
23244 KB |
Output is correct |
53 |
Correct |
127 ms |
22288 KB |
Output is correct |
54 |
Correct |
107 ms |
22868 KB |
Output is correct |
55 |
Correct |
8 ms |
11096 KB |
Output is correct |
56 |
Correct |
8 ms |
11096 KB |
Output is correct |
57 |
Correct |
55 ms |
18028 KB |
Output is correct |
58 |
Correct |
28 ms |
18632 KB |
Output is correct |
59 |
Correct |
66 ms |
24544 KB |
Output is correct |
60 |
Correct |
262 ms |
37192 KB |
Output is correct |
61 |
Correct |
102 ms |
22872 KB |
Output is correct |
62 |
Correct |
128 ms |
24188 KB |
Output is correct |
63 |
Correct |
142 ms |
24044 KB |
Output is correct |
64 |
Correct |
238 ms |
24076 KB |
Output is correct |
65 |
Correct |
139 ms |
24656 KB |
Output is correct |
66 |
Correct |
234 ms |
31764 KB |
Output is correct |
67 |
Correct |
68 ms |
25796 KB |
Output is correct |
68 |
Correct |
134 ms |
25052 KB |
Output is correct |
69 |
Correct |
132 ms |
23096 KB |
Output is correct |
70 |
Correct |
128 ms |
25128 KB |
Output is correct |