#include "race.h"
#pragma once
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<vector<pair<int, int> > > graph;
vector<int> lvl;
vector<long long> lvl_cost;
vector<int> weight;
void predfs(int nod, int tata){
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
lvl[it.first] = 1 + lvl[nod];
lvl_cost[it.first] = it.second + lvl_cost[nod];
predfs(it.first, nod);
weight[nod] += weight[it.first];
}
}
unordered_map<long long, int> mp;
void add(int nod){
if(mp.count(lvl_cost[nod]) == 0){
mp[lvl_cost[nod]] = lvl[nod];
}else{
mp[lvl_cost[nod]] = min(mp[lvl_cost[nod]], lvl[nod]);
}
}
int ans_length = (1 << 30);
void update(long long target, int root, int nod){
if(mp.count(target) == 0){
return ;
}
ans_length = min(ans_length, mp[target] + lvl[nod] - 2 * lvl[root]);
}
void dfs_add(int nod, int tata){
add(nod);
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
dfs_add(it.first, nod);
}
}
void dfs_view(int root, int nod, int tata){
long long target = k - (lvl_cost[nod] - lvl_cost[root]) + lvl_cost[root];
update(target, root, nod);
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
dfs_view(root, it.first, nod);
}
}
void dfs(int nod, int tata, bool dump){
int heavyChild = -1;
for(auto it:graph[nod]){
if(it.first == tata){
continue;
}
if(heavyChild == -1 || weight[it.first] > weight[heavyChild]){
heavyChild = it.first;
}
}
for(auto it:graph[nod]){
if(it.first == tata || it.first == heavyChild){
continue;
}
dfs(it.first, nod, true);
}
if(heavyChild != -1){
dfs(heavyChild, nod, false);
}
add(nod);
for(auto it:graph[nod]){
if(it.first == tata || it.first == heavyChild){
continue;
}
dfs_view(nod, it.first, nod);
dfs_add(it.first, nod);
}
update(lvl_cost[nod] + k, nod, nod);
if(dump){
mp.clear();
}
}
int best_path(int _n, int _k, int h[][2], int l[]){
n = _n;
k = _k;
graph = vector<vector<pair<int,int> > >(n + 1, vector<pair<int, int> >());
lvl = vector<int>(n + 1,0);
lvl_cost = vector<long long>(n + 1, 0);
weight = vector<int>(n + 1, 1);
for(int i = 0;i < n - 1;i++){
graph[h[i][0] + 1].push_back({h[i][1] + 1, l[i]});
graph[h[i][1] + 1].push_back({h[i][0] + 1, l[i]});
}
predfs(1, 0);
dfs(1, 0, true);
return (ans_length == (1 << 30) ? -1:ans_length);
}
Compilation message
race.cpp:3:9: warning: #pragma once in main file
3 | #pragma once
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
448 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
444 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
436 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
452 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
109 ms |
9324 KB |
Output is correct |
20 |
Correct |
125 ms |
10424 KB |
Output is correct |
21 |
Correct |
137 ms |
10424 KB |
Output is correct |
22 |
Correct |
131 ms |
10536 KB |
Output is correct |
23 |
Correct |
113 ms |
10696 KB |
Output is correct |
24 |
Correct |
107 ms |
10768 KB |
Output is correct |
25 |
Correct |
58 ms |
15096 KB |
Output is correct |
26 |
Correct |
45 ms |
19240 KB |
Output is correct |
27 |
Correct |
173 ms |
20944 KB |
Output is correct |
28 |
Correct |
186 ms |
47948 KB |
Output is correct |
29 |
Correct |
186 ms |
46644 KB |
Output is correct |
30 |
Correct |
204 ms |
21068 KB |
Output is correct |
31 |
Correct |
167 ms |
20952 KB |
Output is correct |
32 |
Correct |
252 ms |
21140 KB |
Output is correct |
33 |
Correct |
192 ms |
19960 KB |
Output is correct |
34 |
Correct |
315 ms |
30216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
448 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
444 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
436 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
452 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
109 ms |
9324 KB |
Output is correct |
40 |
Correct |
125 ms |
10424 KB |
Output is correct |
41 |
Correct |
137 ms |
10424 KB |
Output is correct |
42 |
Correct |
131 ms |
10536 KB |
Output is correct |
43 |
Correct |
113 ms |
10696 KB |
Output is correct |
44 |
Correct |
107 ms |
10768 KB |
Output is correct |
45 |
Correct |
58 ms |
15096 KB |
Output is correct |
46 |
Correct |
45 ms |
19240 KB |
Output is correct |
47 |
Correct |
173 ms |
20944 KB |
Output is correct |
48 |
Correct |
186 ms |
47948 KB |
Output is correct |
49 |
Correct |
186 ms |
46644 KB |
Output is correct |
50 |
Correct |
204 ms |
21068 KB |
Output is correct |
51 |
Correct |
167 ms |
20952 KB |
Output is correct |
52 |
Correct |
252 ms |
21140 KB |
Output is correct |
53 |
Correct |
192 ms |
19960 KB |
Output is correct |
54 |
Correct |
315 ms |
30216 KB |
Output is correct |
55 |
Correct |
10 ms |
1600 KB |
Output is correct |
56 |
Correct |
8 ms |
1236 KB |
Output is correct |
57 |
Correct |
82 ms |
10652 KB |
Output is correct |
58 |
Correct |
47 ms |
10324 KB |
Output is correct |
59 |
Correct |
53 ms |
23884 KB |
Output is correct |
60 |
Correct |
173 ms |
46956 KB |
Output is correct |
61 |
Correct |
224 ms |
22312 KB |
Output is correct |
62 |
Correct |
179 ms |
21124 KB |
Output is correct |
63 |
Correct |
238 ms |
21184 KB |
Output is correct |
64 |
Correct |
888 ms |
25996 KB |
Output is correct |
65 |
Correct |
920 ms |
31276 KB |
Output is correct |
66 |
Correct |
231 ms |
43704 KB |
Output is correct |
67 |
Correct |
147 ms |
21728 KB |
Output is correct |
68 |
Correct |
307 ms |
28240 KB |
Output is correct |
69 |
Correct |
318 ms |
28688 KB |
Output is correct |
70 |
Correct |
290 ms |
27148 KB |
Output is correct |