#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> graph[200004];
bool usun[200004];
int siz[200004];
int ans=200004;
map<int,int> mp;
int dlugosc;
void prep(int v, int p){
siz[v]=1;
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
prep(u.first,v);
siz[v]+=siz[u.first];
}
}
int find_centr(int v, int p, int n){
int mx_pod=0,mx_son;
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
if (mx_pod<siz[u.first]){
mx_son=u.first;
mx_pod=siz[u.first];
};
}
if (mx_pod*2>n)return find_centr(mx_son,v,n);
else return v;
}
void dfs1(int v, int p, int odl, int dist){
if (mp.find(dlugosc-odl)!=mp.end())ans=min(ans,mp[dlugosc-odl]+dist);
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
dfs1(u.first,v,odl+u.second,dist+1);
}
}
void dfs2(int v, int p, int odl, int dist){
for (auto u : graph[v]){
if (u.first==p || usun[u.first])continue;
dfs2(u.first,v,odl+u.second,dist+1);
}
if (mp.find(odl)==mp.end())mp[odl]=dist;
else mp[odl]=min(mp[odl],dist);
}
void dekomp(int v){
prep(v,-1);
int centr=find_centr(v,-1,siz[v]);
usun[centr]=true;
mp.clear();
for (auto u : graph[centr]){
if (usun[u.first])continue;
dfs1(u.first,centr,u.second,1);
dfs2(u.first,centr,u.second,1);
}
if (mp.find(dlugosc)!=mp.end())ans=min(ans,mp[dlugosc]);
for (auto u : graph[centr]){
if (usun[u.first])continue;
dekomp(u.first);
}
}
int best_path(int n, int k, int h[][2], int l[]){
dlugosc=k;
for (int i = 0; i<n-1; i++){
graph[h[i][0]].push_back({h[i][1],l[i]});
graph[h[i][1]].push_back({h[i][0],l[i]});
}
dekomp(0);
if (ans>200000)return -1;
return ans;
}
Compilation message
race.cpp: In function 'int find_centr(int, int, int)':
race.cpp:21:15: warning: 'mx_son' may be used uninitialized in this function [-Wmaybe-uninitialized]
21 | int mx_pod=0,mx_son;
| ^~~~~~
race.cpp: In function 'void dekomp(int)':
race.cpp:58:7: warning: 'mx_son' may be used uninitialized in this function [-Wmaybe-uninitialized]
58 | dfs2(u.first,centr,u.second,1);
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9520 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9560 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9656 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 |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 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 |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9520 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9560 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9656 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 |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 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 |
9656 KB |
Output is correct |
22 |
Correct |
3 ms |
9816 KB |
Output is correct |
23 |
Correct |
3 ms |
9820 KB |
Output is correct |
24 |
Correct |
3 ms |
9820 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9820 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9820 KB |
Output is correct |
30 |
Correct |
3 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9816 KB |
Output is correct |
32 |
Correct |
3 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9744 KB |
Output is correct |
34 |
Correct |
4 ms |
9820 KB |
Output is correct |
35 |
Correct |
3 ms |
9820 KB |
Output is correct |
36 |
Correct |
3 ms |
9820 KB |
Output is correct |
37 |
Correct |
3 ms |
9840 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 |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9520 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9560 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9656 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 |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 KB |
Output is correct |
19 |
Correct |
167 ms |
15628 KB |
Output is correct |
20 |
Correct |
162 ms |
15444 KB |
Output is correct |
21 |
Correct |
156 ms |
15812 KB |
Output is correct |
22 |
Correct |
134 ms |
15704 KB |
Output is correct |
23 |
Correct |
210 ms |
15708 KB |
Output is correct |
24 |
Correct |
103 ms |
15956 KB |
Output is correct |
25 |
Correct |
143 ms |
19800 KB |
Output is correct |
26 |
Correct |
81 ms |
22708 KB |
Output is correct |
27 |
Correct |
203 ms |
19540 KB |
Output is correct |
28 |
Correct |
614 ms |
43348 KB |
Output is correct |
29 |
Correct |
641 ms |
43996 KB |
Output is correct |
30 |
Correct |
213 ms |
21856 KB |
Output is correct |
31 |
Correct |
213 ms |
22100 KB |
Output is correct |
32 |
Correct |
227 ms |
21780 KB |
Output is correct |
33 |
Correct |
366 ms |
20768 KB |
Output is correct |
34 |
Correct |
562 ms |
30308 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 |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9520 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9560 KB |
Output is correct |
7 |
Correct |
2 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9656 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 |
9564 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9560 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 |
9656 KB |
Output is correct |
22 |
Correct |
3 ms |
9816 KB |
Output is correct |
23 |
Correct |
3 ms |
9820 KB |
Output is correct |
24 |
Correct |
3 ms |
9820 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
3 ms |
9820 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9820 KB |
Output is correct |
30 |
Correct |
3 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9816 KB |
Output is correct |
32 |
Correct |
3 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9744 KB |
Output is correct |
34 |
Correct |
4 ms |
9820 KB |
Output is correct |
35 |
Correct |
3 ms |
9820 KB |
Output is correct |
36 |
Correct |
3 ms |
9820 KB |
Output is correct |
37 |
Correct |
3 ms |
9840 KB |
Output is correct |
38 |
Correct |
3 ms |
9820 KB |
Output is correct |
39 |
Correct |
167 ms |
15628 KB |
Output is correct |
40 |
Correct |
162 ms |
15444 KB |
Output is correct |
41 |
Correct |
156 ms |
15812 KB |
Output is correct |
42 |
Correct |
134 ms |
15704 KB |
Output is correct |
43 |
Correct |
210 ms |
15708 KB |
Output is correct |
44 |
Correct |
103 ms |
15956 KB |
Output is correct |
45 |
Correct |
143 ms |
19800 KB |
Output is correct |
46 |
Correct |
81 ms |
22708 KB |
Output is correct |
47 |
Correct |
203 ms |
19540 KB |
Output is correct |
48 |
Correct |
614 ms |
43348 KB |
Output is correct |
49 |
Correct |
641 ms |
43996 KB |
Output is correct |
50 |
Correct |
213 ms |
21856 KB |
Output is correct |
51 |
Correct |
213 ms |
22100 KB |
Output is correct |
52 |
Correct |
227 ms |
21780 KB |
Output is correct |
53 |
Correct |
366 ms |
20768 KB |
Output is correct |
54 |
Correct |
562 ms |
30308 KB |
Output is correct |
55 |
Correct |
17 ms |
10332 KB |
Output is correct |
56 |
Correct |
11 ms |
10164 KB |
Output is correct |
57 |
Correct |
102 ms |
16068 KB |
Output is correct |
58 |
Correct |
35 ms |
16844 KB |
Output is correct |
59 |
Correct |
217 ms |
27216 KB |
Output is correct |
60 |
Correct |
608 ms |
43672 KB |
Output is correct |
61 |
Correct |
249 ms |
22252 KB |
Output is correct |
62 |
Correct |
199 ms |
21756 KB |
Output is correct |
63 |
Correct |
221 ms |
21768 KB |
Output is correct |
64 |
Correct |
669 ms |
24828 KB |
Output is correct |
65 |
Correct |
593 ms |
31872 KB |
Output is correct |
66 |
Correct |
614 ms |
39508 KB |
Output is correct |
67 |
Correct |
84 ms |
23176 KB |
Output is correct |
68 |
Correct |
322 ms |
29924 KB |
Output is correct |
69 |
Correct |
343 ms |
28016 KB |
Output is correct |
70 |
Correct |
321 ms |
29616 KB |
Output is correct |