#include "race.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAX = 2e5 + 5;
vector<pii> g[MAX];
ll dist[MAX];
ll height[MAX];
int k = 0;
void dfs1(int node, int p, int h, ll d){
dist[node] = d;
height[node] = h;
for(pii to:g[node]){
if(to.first == p) continue;
dfs1(to.first, node, h + 1, d + to.second);
}
}
int search(set<pair<ll, int>>& s, ll d){
auto itr = s.lower_bound({d, 0});
if(itr != s.end()){
if((itr->first) == d){
return itr->second;
}
else{
return -1;
}
}
else{
return -1;
}
}
ll ans = MAX;
set<pair<ll, int>> sets[MAX];
void dfs2(int node, int p){
for(pii to:g[node]){
if(to.first == p) continue;
dfs2(to.first, node);
int h = search(sets[to.first], dist[node] + k);
if(h != -1) ans = min(ans, h - height[node]);
if(sets[to.first].size() > sets[node].size()){
swap(sets[to.first], sets[node]);
}
}
for(pii to:g[node]){
if(to.first == p) continue;
for(auto& p:sets[to.first]){
int h = search(sets[node], k - p.first + (dist[node] << 1));
if(h != -1) ans = min(ans, h + p.second - (height[node] << 1));
}
for(auto& p:sets[to.first]){
sets[node].insert(p);
}
}
sets[node].insert({dist[node], height[node]});
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
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]});
}
dfs1(0, 0, 0, 0);
dfs2(0, 0);
if(ans == MAX) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 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 |
14444 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14340 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 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 |
14444 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14340 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 |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
8 ms |
14612 KB |
Output is correct |
24 |
Correct |
9 ms |
14664 KB |
Output is correct |
25 |
Correct |
10 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 |
8 ms |
14676 KB |
Output is correct |
29 |
Correct |
8 ms |
14632 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
8 ms |
14684 KB |
Output is correct |
32 |
Correct |
8 ms |
14676 KB |
Output is correct |
33 |
Correct |
8 ms |
14676 KB |
Output is correct |
34 |
Correct |
8 ms |
14548 KB |
Output is correct |
35 |
Correct |
7 ms |
14548 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 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 |
14444 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14340 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
137 ms |
36500 KB |
Output is correct |
20 |
Correct |
125 ms |
36428 KB |
Output is correct |
21 |
Correct |
136 ms |
36116 KB |
Output is correct |
22 |
Correct |
126 ms |
35356 KB |
Output is correct |
23 |
Correct |
152 ms |
50288 KB |
Output is correct |
24 |
Correct |
115 ms |
39148 KB |
Output is correct |
25 |
Correct |
113 ms |
37700 KB |
Output is correct |
26 |
Correct |
77 ms |
42060 KB |
Output is correct |
27 |
Correct |
195 ms |
43308 KB |
Output is correct |
28 |
Correct |
233 ms |
72884 KB |
Output is correct |
29 |
Correct |
252 ms |
71544 KB |
Output is correct |
30 |
Correct |
200 ms |
46284 KB |
Output is correct |
31 |
Correct |
204 ms |
46132 KB |
Output is correct |
32 |
Correct |
260 ms |
46292 KB |
Output is correct |
33 |
Correct |
203 ms |
41888 KB |
Output is correct |
34 |
Correct |
311 ms |
74788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 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 |
14444 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14340 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 |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
8 ms |
14612 KB |
Output is correct |
24 |
Correct |
9 ms |
14664 KB |
Output is correct |
25 |
Correct |
10 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 |
8 ms |
14676 KB |
Output is correct |
29 |
Correct |
8 ms |
14632 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
8 ms |
14684 KB |
Output is correct |
32 |
Correct |
8 ms |
14676 KB |
Output is correct |
33 |
Correct |
8 ms |
14676 KB |
Output is correct |
34 |
Correct |
8 ms |
14548 KB |
Output is correct |
35 |
Correct |
7 ms |
14548 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
39 |
Correct |
137 ms |
36500 KB |
Output is correct |
40 |
Correct |
125 ms |
36428 KB |
Output is correct |
41 |
Correct |
136 ms |
36116 KB |
Output is correct |
42 |
Correct |
126 ms |
35356 KB |
Output is correct |
43 |
Correct |
152 ms |
50288 KB |
Output is correct |
44 |
Correct |
115 ms |
39148 KB |
Output is correct |
45 |
Correct |
113 ms |
37700 KB |
Output is correct |
46 |
Correct |
77 ms |
42060 KB |
Output is correct |
47 |
Correct |
195 ms |
43308 KB |
Output is correct |
48 |
Correct |
233 ms |
72884 KB |
Output is correct |
49 |
Correct |
252 ms |
71544 KB |
Output is correct |
50 |
Correct |
200 ms |
46284 KB |
Output is correct |
51 |
Correct |
204 ms |
46132 KB |
Output is correct |
52 |
Correct |
260 ms |
46292 KB |
Output is correct |
53 |
Correct |
203 ms |
41888 KB |
Output is correct |
54 |
Correct |
311 ms |
74788 KB |
Output is correct |
55 |
Correct |
20 ms |
17492 KB |
Output is correct |
56 |
Correct |
14 ms |
16252 KB |
Output is correct |
57 |
Correct |
73 ms |
33564 KB |
Output is correct |
58 |
Correct |
49 ms |
28336 KB |
Output is correct |
59 |
Correct |
77 ms |
43420 KB |
Output is correct |
60 |
Correct |
235 ms |
71924 KB |
Output is correct |
61 |
Correct |
223 ms |
52068 KB |
Output is correct |
62 |
Correct |
189 ms |
46312 KB |
Output is correct |
63 |
Correct |
263 ms |
46332 KB |
Output is correct |
64 |
Correct |
396 ms |
100116 KB |
Output is correct |
65 |
Correct |
383 ms |
94572 KB |
Output is correct |
66 |
Correct |
244 ms |
68544 KB |
Output is correct |
67 |
Correct |
173 ms |
43676 KB |
Output is correct |
68 |
Correct |
313 ms |
63588 KB |
Output is correct |
69 |
Correct |
328 ms |
64172 KB |
Output is correct |
70 |
Correct |
308 ms |
61492 KB |
Output is correct |