#include "race.h"
#include<bits/stdc++.h>
using namespace std;
vector<set<pair<int64_t,int64_t>>>adj;
int64_t k=0;
vector<int64_t>sub(1e6,0);
vector<int64_t>ans;
vector<int64_t>aux;
int64_t answer = LLONG_MAX;
int64_t kk = -1;
int64_t bookmark = 0;
void dfs (int64_t cur,int64_t par,int64_t cost,int64_t len,int64_t fill){
if (cost>kk)return;
if (fill){
if (aux[cost]<bookmark){
aux[cost] = bookmark;
ans[cost] = len;
}
else if (len<ans[cost]){
ans[cost] = len;
aux[cost] = bookmark;
}
}
else{
if (aux[kk - cost]==bookmark){
if (len + ans[kk - cost]<answer){
answer = min(answer,len + ans[kk-cost]);
}
}
if (cost ==kk){
answer = min(answer,len);
}
}
for (auto x:adj[cur]){
if (x.first!=par){
dfs(x.first,cur,cost + x.second,len + 1,fill);
}
}
}
void getsub(int64_t u,int64_t par){
sub[u]=1;
k++;
for (auto x:adj[u]){
if (x.first!=par){
getsub(x.first,u);
sub[u]+=sub[x.first];
}
}
}
int64_t getcentroid(int64_t u,int64_t par){
for (auto x:adj[u]){
if (x.first!=par){
if (sub[x.first]>k/2){
return getcentroid(x.first,u);
}
}
}
return u;
}
void decomposition(int64_t u,int64_t par){
k = 0;
getsub(u,-1);
int64_t c = getcentroid(u,-1);
bookmark++;
for (auto x:adj[c]){
dfs(x.first,c,x.second,1,0);
dfs(x.first,c,x.second,1,1);
}
for (auto x:adj[c]){
adj[x.first].erase({c,x.second});
//adj[c].erase(x);
decomposition(x.first,c);
}
}
void build(){
decomposition(0,-1);
}
int best_path(int N, int K, int H[][2], int L[])
{
aux.resize(K + 1,0);
ans.resize(K + 1,0);
adj.resize(N + 1);
kk = K;
for (int64_t i = 0;i<N-1;++i){
adj[H[i][0]].insert({H[i][1],L[i]});
adj[H[i][1]].insert({H[i][0],L[i]});
}
build();
if (answer==LLONG_MAX){
return -1;
}
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8140 KB |
Output is correct |
2 |
Correct |
3 ms |
8140 KB |
Output is correct |
3 |
Correct |
3 ms |
8140 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8140 KB |
Output is correct |
6 |
Correct |
4 ms |
8140 KB |
Output is correct |
7 |
Correct |
3 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
3 ms |
8140 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
3 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8140 KB |
Output is correct |
16 |
Correct |
4 ms |
8140 KB |
Output is correct |
17 |
Correct |
3 ms |
8140 KB |
Output is correct |
18 |
Correct |
4 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8140 KB |
Output is correct |
2 |
Correct |
3 ms |
8140 KB |
Output is correct |
3 |
Correct |
3 ms |
8140 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8140 KB |
Output is correct |
6 |
Correct |
4 ms |
8140 KB |
Output is correct |
7 |
Correct |
3 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
3 ms |
8140 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
3 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8140 KB |
Output is correct |
16 |
Correct |
4 ms |
8140 KB |
Output is correct |
17 |
Correct |
3 ms |
8140 KB |
Output is correct |
18 |
Correct |
4 ms |
8140 KB |
Output is correct |
19 |
Correct |
4 ms |
8140 KB |
Output is correct |
20 |
Correct |
3 ms |
8140 KB |
Output is correct |
21 |
Correct |
4 ms |
8268 KB |
Output is correct |
22 |
Correct |
11 ms |
22640 KB |
Output is correct |
23 |
Correct |
12 ms |
20172 KB |
Output is correct |
24 |
Correct |
11 ms |
21888 KB |
Output is correct |
25 |
Correct |
11 ms |
21484 KB |
Output is correct |
26 |
Correct |
6 ms |
13516 KB |
Output is correct |
27 |
Correct |
10 ms |
20804 KB |
Output is correct |
28 |
Correct |
5 ms |
11468 KB |
Output is correct |
29 |
Correct |
7 ms |
13424 KB |
Output is correct |
30 |
Correct |
7 ms |
14096 KB |
Output is correct |
31 |
Correct |
9 ms |
18552 KB |
Output is correct |
32 |
Correct |
9 ms |
19416 KB |
Output is correct |
33 |
Correct |
10 ms |
20592 KB |
Output is correct |
34 |
Correct |
8 ms |
17484 KB |
Output is correct |
35 |
Correct |
10 ms |
20940 KB |
Output is correct |
36 |
Correct |
10 ms |
22868 KB |
Output is correct |
37 |
Correct |
9 ms |
20684 KB |
Output is correct |
38 |
Correct |
8 ms |
16304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8140 KB |
Output is correct |
2 |
Correct |
3 ms |
8140 KB |
Output is correct |
3 |
Correct |
3 ms |
8140 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8140 KB |
Output is correct |
6 |
Correct |
4 ms |
8140 KB |
Output is correct |
7 |
Correct |
3 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
3 ms |
8140 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
3 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8140 KB |
Output is correct |
16 |
Correct |
4 ms |
8140 KB |
Output is correct |
17 |
Correct |
3 ms |
8140 KB |
Output is correct |
18 |
Correct |
4 ms |
8140 KB |
Output is correct |
19 |
Correct |
195 ms |
28056 KB |
Output is correct |
20 |
Correct |
209 ms |
27960 KB |
Output is correct |
21 |
Correct |
218 ms |
27844 KB |
Output is correct |
22 |
Correct |
239 ms |
27988 KB |
Output is correct |
23 |
Correct |
119 ms |
28032 KB |
Output is correct |
24 |
Correct |
91 ms |
27992 KB |
Output is correct |
25 |
Correct |
201 ms |
31044 KB |
Output is correct |
26 |
Correct |
125 ms |
34124 KB |
Output is correct |
27 |
Correct |
290 ms |
47492 KB |
Output is correct |
28 |
Correct |
388 ms |
59960 KB |
Output is correct |
29 |
Correct |
383 ms |
59184 KB |
Output is correct |
30 |
Correct |
277 ms |
47428 KB |
Output is correct |
31 |
Correct |
282 ms |
47468 KB |
Output is correct |
32 |
Correct |
325 ms |
47300 KB |
Output is correct |
33 |
Correct |
324 ms |
47748 KB |
Output is correct |
34 |
Correct |
283 ms |
47364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8140 KB |
Output is correct |
2 |
Correct |
3 ms |
8140 KB |
Output is correct |
3 |
Correct |
3 ms |
8140 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8140 KB |
Output is correct |
6 |
Correct |
4 ms |
8140 KB |
Output is correct |
7 |
Correct |
3 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
3 ms |
8140 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
3 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8140 KB |
Output is correct |
16 |
Correct |
4 ms |
8140 KB |
Output is correct |
17 |
Correct |
3 ms |
8140 KB |
Output is correct |
18 |
Correct |
4 ms |
8140 KB |
Output is correct |
19 |
Correct |
4 ms |
8140 KB |
Output is correct |
20 |
Correct |
3 ms |
8140 KB |
Output is correct |
21 |
Correct |
4 ms |
8268 KB |
Output is correct |
22 |
Correct |
11 ms |
22640 KB |
Output is correct |
23 |
Correct |
12 ms |
20172 KB |
Output is correct |
24 |
Correct |
11 ms |
21888 KB |
Output is correct |
25 |
Correct |
11 ms |
21484 KB |
Output is correct |
26 |
Correct |
6 ms |
13516 KB |
Output is correct |
27 |
Correct |
10 ms |
20804 KB |
Output is correct |
28 |
Correct |
5 ms |
11468 KB |
Output is correct |
29 |
Correct |
7 ms |
13424 KB |
Output is correct |
30 |
Correct |
7 ms |
14096 KB |
Output is correct |
31 |
Correct |
9 ms |
18552 KB |
Output is correct |
32 |
Correct |
9 ms |
19416 KB |
Output is correct |
33 |
Correct |
10 ms |
20592 KB |
Output is correct |
34 |
Correct |
8 ms |
17484 KB |
Output is correct |
35 |
Correct |
10 ms |
20940 KB |
Output is correct |
36 |
Correct |
10 ms |
22868 KB |
Output is correct |
37 |
Correct |
9 ms |
20684 KB |
Output is correct |
38 |
Correct |
8 ms |
16304 KB |
Output is correct |
39 |
Correct |
195 ms |
28056 KB |
Output is correct |
40 |
Correct |
209 ms |
27960 KB |
Output is correct |
41 |
Correct |
218 ms |
27844 KB |
Output is correct |
42 |
Correct |
239 ms |
27988 KB |
Output is correct |
43 |
Correct |
119 ms |
28032 KB |
Output is correct |
44 |
Correct |
91 ms |
27992 KB |
Output is correct |
45 |
Correct |
201 ms |
31044 KB |
Output is correct |
46 |
Correct |
125 ms |
34124 KB |
Output is correct |
47 |
Correct |
290 ms |
47492 KB |
Output is correct |
48 |
Correct |
388 ms |
59960 KB |
Output is correct |
49 |
Correct |
383 ms |
59184 KB |
Output is correct |
50 |
Correct |
277 ms |
47428 KB |
Output is correct |
51 |
Correct |
282 ms |
47468 KB |
Output is correct |
52 |
Correct |
325 ms |
47300 KB |
Output is correct |
53 |
Correct |
324 ms |
47748 KB |
Output is correct |
54 |
Correct |
283 ms |
47364 KB |
Output is correct |
55 |
Correct |
14 ms |
10080 KB |
Output is correct |
56 |
Correct |
14 ms |
10060 KB |
Output is correct |
57 |
Correct |
109 ms |
27856 KB |
Output is correct |
58 |
Correct |
69 ms |
27488 KB |
Output is correct |
59 |
Correct |
136 ms |
37104 KB |
Output is correct |
60 |
Correct |
655 ms |
75348 KB |
Output is correct |
61 |
Correct |
301 ms |
47428 KB |
Output is correct |
62 |
Correct |
313 ms |
63044 KB |
Output is correct |
63 |
Correct |
410 ms |
63100 KB |
Output is correct |
64 |
Correct |
619 ms |
55748 KB |
Output is correct |
65 |
Correct |
346 ms |
47668 KB |
Output is correct |
66 |
Correct |
523 ms |
72772 KB |
Output is correct |
67 |
Correct |
330 ms |
63276 KB |
Output is correct |
68 |
Correct |
427 ms |
62420 KB |
Output is correct |
69 |
Correct |
431 ms |
62820 KB |
Output is correct |
70 |
Correct |
376 ms |
60796 KB |
Output is correct |