#include "race.h"
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>>adj[200001];
int dsu[200001],sz[200001];
map<long long,int> store[200001];
long long sum[200001];
long long ans = INT_MAX, K;
int Find(int x){
return x == dsu[x] ? x : dsu[x] = Find(dsu[x]);
}
void Unite(int a, int b, long long cur_k, int cur_depth){
a = Find(a); b = Find(b);
if(sz[a] > sz[b]) swap(a,b);
sz[b] += sz[a];
dsu[a] = b;
for(auto &s: store[a]){
if(store[b].count(cur_k - s.first)){
ans = min(ans, (long long)s.second - cur_depth + store[b][cur_k - s.first] - cur_depth);
}
}
for(auto &s: store[a]){
if(!store[b].count(s.first) or store[b][s.first] > s.second){
store[b][s.first] = s.second;
}
}
}
void dfs(int x, int par, int depth){
for(auto s: adj[x]){
if(s.first != par){
sum[s.first] = s.second + sum[x];
dfs(s.first, x, depth + 1);
}
}
store[x][sum[x]] = depth;
for(auto s: adj[x]){
if(s.first != par){
Unite(x, s.first, 2ll * sum[x] + K, depth);
}
}
int b = Find(x);
if(store[b].count(sum[x] + K) != 0) ans = min(ans, (long long)store[b][sum[x] + K] - depth);
}
signed best_path(signed n, signed k, signed h[][2], signed l[]){
for (int i = 1 ; i <= n ; i++) sz[i] = 1, dsu[i] = i;
ans = INT_MAX;
for(int i = 0; i < n - 1; i++){
h[i][0]++; h[i][1]++;
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
K = k;
dfs(1, 0, 1);
if(ans == INT_MAX) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14384 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14344 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14440 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14372 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14452 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14384 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14344 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14440 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14372 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14452 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14536 KB |
Output is correct |
21 |
Correct |
8 ms |
14632 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
8 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
14692 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14572 KB |
Output is correct |
28 |
Correct |
8 ms |
14676 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
8 ms |
14676 KB |
Output is correct |
32 |
Correct |
9 ms |
14612 KB |
Output is correct |
33 |
Correct |
9 ms |
14684 KB |
Output is correct |
34 |
Correct |
8 ms |
14560 KB |
Output is correct |
35 |
Correct |
9 ms |
14548 KB |
Output is correct |
36 |
Correct |
9 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14648 KB |
Output is correct |
38 |
Correct |
8 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14384 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14344 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14440 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14372 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14452 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
116 ms |
36428 KB |
Output is correct |
20 |
Correct |
118 ms |
36456 KB |
Output is correct |
21 |
Correct |
113 ms |
36228 KB |
Output is correct |
22 |
Correct |
118 ms |
35808 KB |
Output is correct |
23 |
Correct |
162 ms |
48680 KB |
Output is correct |
24 |
Correct |
118 ms |
39148 KB |
Output is correct |
25 |
Correct |
92 ms |
33740 KB |
Output is correct |
26 |
Correct |
59 ms |
38988 KB |
Output is correct |
27 |
Correct |
189 ms |
46496 KB |
Output is correct |
28 |
Correct |
323 ms |
76108 KB |
Output is correct |
29 |
Correct |
330 ms |
75220 KB |
Output is correct |
30 |
Correct |
184 ms |
46508 KB |
Output is correct |
31 |
Correct |
183 ms |
46472 KB |
Output is correct |
32 |
Correct |
251 ms |
46592 KB |
Output is correct |
33 |
Correct |
237 ms |
51532 KB |
Output is correct |
34 |
Correct |
330 ms |
83404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14384 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14344 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14440 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14372 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14452 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14536 KB |
Output is correct |
21 |
Correct |
8 ms |
14632 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
8 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
14692 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14572 KB |
Output is correct |
28 |
Correct |
8 ms |
14676 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
8 ms |
14676 KB |
Output is correct |
32 |
Correct |
9 ms |
14612 KB |
Output is correct |
33 |
Correct |
9 ms |
14684 KB |
Output is correct |
34 |
Correct |
8 ms |
14560 KB |
Output is correct |
35 |
Correct |
9 ms |
14548 KB |
Output is correct |
36 |
Correct |
9 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14648 KB |
Output is correct |
38 |
Correct |
8 ms |
14676 KB |
Output is correct |
39 |
Correct |
116 ms |
36428 KB |
Output is correct |
40 |
Correct |
118 ms |
36456 KB |
Output is correct |
41 |
Correct |
113 ms |
36228 KB |
Output is correct |
42 |
Correct |
118 ms |
35808 KB |
Output is correct |
43 |
Correct |
162 ms |
48680 KB |
Output is correct |
44 |
Correct |
118 ms |
39148 KB |
Output is correct |
45 |
Correct |
92 ms |
33740 KB |
Output is correct |
46 |
Correct |
59 ms |
38988 KB |
Output is correct |
47 |
Correct |
189 ms |
46496 KB |
Output is correct |
48 |
Correct |
323 ms |
76108 KB |
Output is correct |
49 |
Correct |
330 ms |
75220 KB |
Output is correct |
50 |
Correct |
184 ms |
46508 KB |
Output is correct |
51 |
Correct |
183 ms |
46472 KB |
Output is correct |
52 |
Correct |
251 ms |
46592 KB |
Output is correct |
53 |
Correct |
237 ms |
51532 KB |
Output is correct |
54 |
Correct |
330 ms |
83404 KB |
Output is correct |
55 |
Correct |
20 ms |
17492 KB |
Output is correct |
56 |
Correct |
14 ms |
16212 KB |
Output is correct |
57 |
Correct |
76 ms |
33816 KB |
Output is correct |
58 |
Correct |
48 ms |
27328 KB |
Output is correct |
59 |
Correct |
98 ms |
45360 KB |
Output is correct |
60 |
Correct |
300 ms |
75612 KB |
Output is correct |
61 |
Correct |
214 ms |
49868 KB |
Output is correct |
62 |
Correct |
176 ms |
46440 KB |
Output is correct |
63 |
Correct |
254 ms |
46528 KB |
Output is correct |
64 |
Correct |
432 ms |
101196 KB |
Output is correct |
65 |
Correct |
415 ms |
99064 KB |
Output is correct |
66 |
Correct |
311 ms |
73088 KB |
Output is correct |
67 |
Correct |
166 ms |
40504 KB |
Output is correct |
68 |
Correct |
330 ms |
62824 KB |
Output is correct |
69 |
Correct |
352 ms |
67828 KB |
Output is correct |
70 |
Correct |
315 ms |
60820 KB |
Output is correct |