//#pragma once
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 3;
int k;
vector<pair<int,int>>adj[N];
bool marked[N];
int sub[N];
int sz;
int ans = 1e9;
int dfs_subtree(int node, int prev){
sub[node] = 1;
for(auto& edge : adj[node]){
if(edge.first != prev && !marked[edge.first]){
sub[node] += dfs_subtree(edge.first, node);
}
}
return sub[node];
}
int find_centroid(int node, int prev){
for(auto& edge : adj[node]){
if(edge.first != prev && !marked[edge.first] && 2 * sub[edge.first] > sz){
return find_centroid(edge.first, node);
}
}
return node;
}
map<int,int>mp{};
vector<pair<int,int>>vc;
void dfs( int node, int prev, int cur, int len){
if(cur>k)return;
vc.push_back({cur,len});
auto it = mp.find(k - cur);
if(it != mp.end()){
ans = min(ans, it->second+len);
}
for(auto & edge : adj[node]){
if(edge.first != prev && !marked[edge.first]){
dfs(edge.first, node, cur + edge.second, len + 1);
}
}
}
void decompose(int node){
dfs_subtree(node, -1);
sz = sub[node];
int c = find_centroid(node, -1);
mp[0]=0;
for(auto&edge:adj[c]){
int child = edge.first;
if(!marked[child]){
vc.clear();
dfs(child, c, edge.second, 1);
for(auto&p:vc){
auto it = mp.find(p.first);
if(it==mp.end()){
mp[p.first]=p.second;
}else{
it->second=min(it->second, p.second);
}
}
}
}
marked[c] = 1;
mp.clear();
for(auto&edge:adj[c]){
if(!marked[edge.first])decompose(edge.first);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for(int i = 0; i < N - 1; i++){
adj[H[i][0]].push_back({H[i][1],L[i]});
adj[H[i][1]].push_back({H[i][0],L[i]});
}
decompose(0);
if(ans == 1e9) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9648 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9716 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9740 KB |
Output is correct |
12 |
Correct |
2 ms |
9716 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9560 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9648 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9716 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9740 KB |
Output is correct |
12 |
Correct |
2 ms |
9716 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9560 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9652 KB |
Output is correct |
29 |
Correct |
3 ms |
10100 KB |
Output is correct |
30 |
Correct |
3 ms |
9656 KB |
Output is correct |
31 |
Correct |
3 ms |
9820 KB |
Output is correct |
32 |
Correct |
3 ms |
9816 KB |
Output is correct |
33 |
Correct |
4 ms |
9820 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
9820 KB |
Output is correct |
37 |
Correct |
3 ms |
9820 KB |
Output is correct |
38 |
Correct |
3 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9648 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9716 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9740 KB |
Output is correct |
12 |
Correct |
2 ms |
9716 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9560 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
104 ms |
15728 KB |
Output is correct |
20 |
Correct |
106 ms |
15696 KB |
Output is correct |
21 |
Correct |
108 ms |
15964 KB |
Output is correct |
22 |
Correct |
116 ms |
16084 KB |
Output is correct |
23 |
Correct |
54 ms |
15696 KB |
Output is correct |
24 |
Correct |
50 ms |
15700 KB |
Output is correct |
25 |
Correct |
85 ms |
18268 KB |
Output is correct |
26 |
Correct |
71 ms |
21972 KB |
Output is correct |
27 |
Correct |
124 ms |
19536 KB |
Output is correct |
28 |
Correct |
174 ms |
30548 KB |
Output is correct |
29 |
Correct |
169 ms |
29780 KB |
Output is correct |
30 |
Correct |
130 ms |
19380 KB |
Output is correct |
31 |
Correct |
135 ms |
19284 KB |
Output is correct |
32 |
Correct |
129 ms |
19740 KB |
Output is correct |
33 |
Correct |
166 ms |
18396 KB |
Output is correct |
34 |
Correct |
110 ms |
18260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9648 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9716 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9740 KB |
Output is correct |
12 |
Correct |
2 ms |
9716 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9560 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9564 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9652 KB |
Output is correct |
29 |
Correct |
3 ms |
10100 KB |
Output is correct |
30 |
Correct |
3 ms |
9656 KB |
Output is correct |
31 |
Correct |
3 ms |
9820 KB |
Output is correct |
32 |
Correct |
3 ms |
9816 KB |
Output is correct |
33 |
Correct |
4 ms |
9820 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
9820 KB |
Output is correct |
37 |
Correct |
3 ms |
9820 KB |
Output is correct |
38 |
Correct |
3 ms |
9820 KB |
Output is correct |
39 |
Correct |
104 ms |
15728 KB |
Output is correct |
40 |
Correct |
106 ms |
15696 KB |
Output is correct |
41 |
Correct |
108 ms |
15964 KB |
Output is correct |
42 |
Correct |
116 ms |
16084 KB |
Output is correct |
43 |
Correct |
54 ms |
15696 KB |
Output is correct |
44 |
Correct |
50 ms |
15700 KB |
Output is correct |
45 |
Correct |
85 ms |
18268 KB |
Output is correct |
46 |
Correct |
71 ms |
21972 KB |
Output is correct |
47 |
Correct |
124 ms |
19536 KB |
Output is correct |
48 |
Correct |
174 ms |
30548 KB |
Output is correct |
49 |
Correct |
169 ms |
29780 KB |
Output is correct |
50 |
Correct |
130 ms |
19380 KB |
Output is correct |
51 |
Correct |
135 ms |
19284 KB |
Output is correct |
52 |
Correct |
129 ms |
19740 KB |
Output is correct |
53 |
Correct |
166 ms |
18396 KB |
Output is correct |
54 |
Correct |
110 ms |
18260 KB |
Output is correct |
55 |
Correct |
14 ms |
10076 KB |
Output is correct |
56 |
Correct |
10 ms |
10264 KB |
Output is correct |
57 |
Correct |
87 ms |
16448 KB |
Output is correct |
58 |
Correct |
34 ms |
15812 KB |
Output is correct |
59 |
Correct |
189 ms |
25040 KB |
Output is correct |
60 |
Correct |
593 ms |
39472 KB |
Output is correct |
61 |
Correct |
164 ms |
19652 KB |
Output is correct |
62 |
Correct |
176 ms |
19688 KB |
Output is correct |
63 |
Correct |
192 ms |
19700 KB |
Output is correct |
64 |
Correct |
613 ms |
24352 KB |
Output is correct |
65 |
Correct |
135 ms |
19648 KB |
Output is correct |
66 |
Correct |
373 ms |
27904 KB |
Output is correct |
67 |
Correct |
86 ms |
19840 KB |
Output is correct |
68 |
Correct |
285 ms |
23308 KB |
Output is correct |
69 |
Correct |
286 ms |
23192 KB |
Output is correct |
70 |
Correct |
275 ms |
23124 KB |
Output is correct |