#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<pair<int,int>>adj[200001];
int dsu[200001],sz[200001];
int inbut[200001][2], ll[200001];
map<int,int> store[200001];
int vs[200001],sum[200001];
int ans = INT_MAX, K;
int Find(int x){
if(dsu[x] == x) return x;
else return dsu[x] = Find(dsu[x]);
}
void Unite(int a, int b, int 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) != 0){
ans = min(ans, s.second - cur_depth + store[b][cur_k - s.first] - cur_depth);
}
}
for(auto s: store[a]){
if(store[b].count(s.first) == 0 or store[b][s.first] > s.second){
store[b][s.first] = s.second;
}
}
}
void dfs(int x, int par, int depth){
vs[x] = 1;
int vv = 0;
for(auto s: adj[x]){
if(!vs[s.first]){
sum[s.first] += s.second;
sum[s.first] += sum[x];
dfs(s.first, x, depth + 1);
vv++;
}
}
map<int,int>p;
p[sum[x]] = depth;
store[x] = p;
if(vv){
for(auto s: adj[x]){
if(s.first != par){
Unite(x, s.first, 2 * sum[x] + K, depth);
}
}
}
int b = Find(x);
if(store[b].count(sum[x] + K) != 0) ans = min(ans, store[b][sum[x] + K] - depth);
}
signed best_path(signed n, signed k, signed h[][2], signed l[]){
ans = INT_MAX;
for(int i = 1; i <= n; i++){
sum[i] = 0;
vs[i] = 0;
dsu[i] = i;
sz[i] = 1;
adj[i].clear();
}
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 |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
9 ms |
14404 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 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 |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14400 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14436 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
9 ms |
14404 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 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 |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14400 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14436 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14404 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
9 ms |
14708 KB |
Output is correct |
23 |
Correct |
8 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
9 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
9 ms |
14680 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
9 ms |
14676 KB |
Output is correct |
32 |
Correct |
8 ms |
14752 KB |
Output is correct |
33 |
Correct |
9 ms |
14804 KB |
Output is correct |
34 |
Correct |
8 ms |
14684 KB |
Output is correct |
35 |
Correct |
9 ms |
14676 KB |
Output is correct |
36 |
Correct |
9 ms |
14688 KB |
Output is correct |
37 |
Correct |
8 ms |
14668 KB |
Output is correct |
38 |
Correct |
8 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
9 ms |
14404 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 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 |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14400 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14436 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
133 ms |
40824 KB |
Output is correct |
20 |
Correct |
138 ms |
40796 KB |
Output is correct |
21 |
Correct |
134 ms |
40500 KB |
Output is correct |
22 |
Correct |
137 ms |
40096 KB |
Output is correct |
23 |
Correct |
171 ms |
53212 KB |
Output is correct |
24 |
Correct |
133 ms |
42964 KB |
Output is correct |
25 |
Correct |
116 ms |
42352 KB |
Output is correct |
26 |
Correct |
75 ms |
51508 KB |
Output is correct |
27 |
Correct |
200 ms |
55692 KB |
Output is correct |
28 |
Correct |
328 ms |
101044 KB |
Output is correct |
29 |
Correct |
336 ms |
99160 KB |
Output is correct |
30 |
Correct |
199 ms |
55696 KB |
Output is correct |
31 |
Correct |
199 ms |
55696 KB |
Output is correct |
32 |
Correct |
290 ms |
56032 KB |
Output is correct |
33 |
Correct |
265 ms |
60860 KB |
Output is correct |
34 |
Correct |
371 ms |
93588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
9 ms |
14404 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 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 |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14400 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14436 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14404 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
9 ms |
14708 KB |
Output is correct |
23 |
Correct |
8 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
9 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
9 ms |
14680 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
9 ms |
14676 KB |
Output is correct |
32 |
Correct |
8 ms |
14752 KB |
Output is correct |
33 |
Correct |
9 ms |
14804 KB |
Output is correct |
34 |
Correct |
8 ms |
14684 KB |
Output is correct |
35 |
Correct |
9 ms |
14676 KB |
Output is correct |
36 |
Correct |
9 ms |
14688 KB |
Output is correct |
37 |
Correct |
8 ms |
14668 KB |
Output is correct |
38 |
Correct |
8 ms |
14676 KB |
Output is correct |
39 |
Correct |
133 ms |
40824 KB |
Output is correct |
40 |
Correct |
138 ms |
40796 KB |
Output is correct |
41 |
Correct |
134 ms |
40500 KB |
Output is correct |
42 |
Correct |
137 ms |
40096 KB |
Output is correct |
43 |
Correct |
171 ms |
53212 KB |
Output is correct |
44 |
Correct |
133 ms |
42964 KB |
Output is correct |
45 |
Correct |
116 ms |
42352 KB |
Output is correct |
46 |
Correct |
75 ms |
51508 KB |
Output is correct |
47 |
Correct |
200 ms |
55692 KB |
Output is correct |
48 |
Correct |
328 ms |
101044 KB |
Output is correct |
49 |
Correct |
336 ms |
99160 KB |
Output is correct |
50 |
Correct |
199 ms |
55696 KB |
Output is correct |
51 |
Correct |
199 ms |
55696 KB |
Output is correct |
52 |
Correct |
290 ms |
56032 KB |
Output is correct |
53 |
Correct |
265 ms |
60860 KB |
Output is correct |
54 |
Correct |
371 ms |
93588 KB |
Output is correct |
55 |
Correct |
21 ms |
18004 KB |
Output is correct |
56 |
Correct |
15 ms |
16656 KB |
Output is correct |
57 |
Correct |
78 ms |
38256 KB |
Output is correct |
58 |
Correct |
54 ms |
30668 KB |
Output is correct |
59 |
Correct |
103 ms |
57588 KB |
Output is correct |
60 |
Correct |
306 ms |
99532 KB |
Output is correct |
61 |
Correct |
224 ms |
59284 KB |
Output is correct |
62 |
Correct |
191 ms |
55760 KB |
Output is correct |
63 |
Correct |
290 ms |
55820 KB |
Output is correct |
64 |
Correct |
483 ms |
110712 KB |
Output is correct |
65 |
Correct |
472 ms |
108696 KB |
Output is correct |
66 |
Correct |
337 ms |
94504 KB |
Output is correct |
67 |
Correct |
208 ms |
48656 KB |
Output is correct |
68 |
Correct |
371 ms |
72148 KB |
Output is correct |
69 |
Correct |
391 ms |
77032 KB |
Output is correct |
70 |
Correct |
346 ms |
69580 KB |
Output is correct |