#include "race.h"
#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e5+5;
int sz[N],dist[2][N],ans,u,v,n,k,vis[N],H;
vector<int>C[N];
pair<int,int> fix[N*10];
vector<pair<int,int> > V[N];
void find_size(int u,int p,int d,int d1,int h){
sz[u]=1;
dist[0][u]=d1;
dist[1][u]=d;
for(int i=0;i<V[u].size();i++){
if(!vis[V[u][i].f] && V[u][i].f!=p){
find_size(V[u][i].f,u,d+V[u][i].s,d1+1,h);
sz[u]+=sz[V[u][i].f];
}
}
}
int find_centroid(int u,int p,int Sz){
for(int i=0;i<V[u].size();i++){
if(!vis[V[u][i].f] && V[u][i].f!=p && Sz/2<sz[V[u][i].f]) {
return find_centroid(V[u][i].f,u,Sz);
}
}
return u;
}
void dfs(int u,int p,int t){
if(k<dist[1][u]) return;
if(t==0){
if(fix[k-dist[1][u]].f==H) ans=min(ans,dist[0][u]+fix[k-dist[1][u]].s);}
else if(t==1){
if(fix[dist[1][u]].f!=H) fix[dist[1][u]].f=H ,fix[dist[1][u]].s = dist[0][u];
else fix[dist[1][u]].s=min(dist[0][u],fix[dist[1][u]].s);
}
for(int i=0;i<V[u].size();i++){
if(!vis[V[u][i].f] && V[u][i].f!=p)dfs(V[u][i].f,u,t);
}
}
void new_tree(int u,int p,int h){
find_size(u,0,0,0,h);
int c=find_centroid(u,0,sz[u]);
find_size(c,0,0,0,h);
vis[c]=h;
fix[0].f=c;
H=c;
for(int i=0;i<V[c].size();i++){
if(!vis[V[c][i].f]){
dfs(V[c][i].f,c,0);
dfs(V[c][i].f,c,1);
}
}
for(int i=0;i<V[c].size();i++){
if(!vis[V[c][i].f]) new_tree(V[c][i].f,c,h+1);
}
}
int best_path(int n, int K, int H[][2], int L[]) {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
k=K;
for(int i=0;i<n-1;i++){
u=H[i][0]; v=H[i][1];
u++; v++;
int w=L[i];
V[u].push_back({v,w});
V[v].push_back({u,w});
}
ans=4e5;
new_tree(1,0,1);
if(ans>n)ans=-1;
return ans;
}
Compilation message
race.cpp: In function 'void find_size(int, int, int, int, int)':
race.cpp:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i=0;i<V[u].size();i++){
| ~^~~~~~~~~~~~
race.cpp: In function 'int find_centroid(int, int, int)':
race.cpp:24:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<V[u].size();i++){
| ~^~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int)':
race.cpp:39:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i=0;i<V[u].size();i++){
| ~^~~~~~~~~~~~
race.cpp: In function 'void new_tree(int, int, int)':
race.cpp:50:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=0;i<V[c].size();i++){
| ~^~~~~~~~~~~~
race.cpp:57:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i=0;i<V[c].size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9836 KB |
Output is correct |
2 |
Correct |
7 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9836 KB |
Output is correct |
5 |
Correct |
7 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9836 KB |
Output is correct |
11 |
Correct |
6 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9836 KB |
Output is correct |
13 |
Correct |
6 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9836 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9836 KB |
Output is correct |
2 |
Correct |
7 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9836 KB |
Output is correct |
5 |
Correct |
7 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9836 KB |
Output is correct |
11 |
Correct |
6 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9836 KB |
Output is correct |
13 |
Correct |
6 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9836 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
6 ms |
9836 KB |
Output is correct |
20 |
Correct |
6 ms |
9836 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
10 ms |
13164 KB |
Output is correct |
23 |
Correct |
10 ms |
12780 KB |
Output is correct |
24 |
Correct |
10 ms |
13932 KB |
Output is correct |
25 |
Correct |
11 ms |
13036 KB |
Output is correct |
26 |
Correct |
9 ms |
12140 KB |
Output is correct |
27 |
Correct |
8 ms |
9836 KB |
Output is correct |
28 |
Correct |
8 ms |
11372 KB |
Output is correct |
29 |
Correct |
9 ms |
12268 KB |
Output is correct |
30 |
Correct |
9 ms |
12524 KB |
Output is correct |
31 |
Correct |
10 ms |
13164 KB |
Output is correct |
32 |
Correct |
10 ms |
13164 KB |
Output is correct |
33 |
Correct |
11 ms |
14572 KB |
Output is correct |
34 |
Correct |
11 ms |
13932 KB |
Output is correct |
35 |
Correct |
11 ms |
14828 KB |
Output is correct |
36 |
Correct |
12 ms |
15212 KB |
Output is correct |
37 |
Correct |
10 ms |
13164 KB |
Output is correct |
38 |
Correct |
9 ms |
12012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9836 KB |
Output is correct |
2 |
Correct |
7 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9836 KB |
Output is correct |
5 |
Correct |
7 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9836 KB |
Output is correct |
11 |
Correct |
6 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9836 KB |
Output is correct |
13 |
Correct |
6 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9836 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
247 ms |
17644 KB |
Output is correct |
20 |
Correct |
243 ms |
17644 KB |
Output is correct |
21 |
Correct |
259 ms |
17516 KB |
Output is correct |
22 |
Correct |
241 ms |
17852 KB |
Output is correct |
23 |
Correct |
148 ms |
17900 KB |
Output is correct |
24 |
Correct |
92 ms |
17772 KB |
Output is correct |
25 |
Correct |
216 ms |
21100 KB |
Output is correct |
26 |
Correct |
133 ms |
24812 KB |
Output is correct |
27 |
Correct |
236 ms |
25708 KB |
Output is correct |
28 |
Correct |
587 ms |
40172 KB |
Output is correct |
29 |
Correct |
577 ms |
39020 KB |
Output is correct |
30 |
Correct |
249 ms |
25964 KB |
Output is correct |
31 |
Correct |
242 ms |
25708 KB |
Output is correct |
32 |
Correct |
359 ms |
25836 KB |
Output is correct |
33 |
Correct |
443 ms |
24812 KB |
Output is correct |
34 |
Correct |
397 ms |
25452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9836 KB |
Output is correct |
2 |
Correct |
7 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9836 KB |
Output is correct |
5 |
Correct |
7 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9836 KB |
Output is correct |
11 |
Correct |
6 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9836 KB |
Output is correct |
13 |
Correct |
6 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9836 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
6 ms |
9836 KB |
Output is correct |
20 |
Correct |
6 ms |
9836 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
10 ms |
13164 KB |
Output is correct |
23 |
Correct |
10 ms |
12780 KB |
Output is correct |
24 |
Correct |
10 ms |
13932 KB |
Output is correct |
25 |
Correct |
11 ms |
13036 KB |
Output is correct |
26 |
Correct |
9 ms |
12140 KB |
Output is correct |
27 |
Correct |
8 ms |
9836 KB |
Output is correct |
28 |
Correct |
8 ms |
11372 KB |
Output is correct |
29 |
Correct |
9 ms |
12268 KB |
Output is correct |
30 |
Correct |
9 ms |
12524 KB |
Output is correct |
31 |
Correct |
10 ms |
13164 KB |
Output is correct |
32 |
Correct |
10 ms |
13164 KB |
Output is correct |
33 |
Correct |
11 ms |
14572 KB |
Output is correct |
34 |
Correct |
11 ms |
13932 KB |
Output is correct |
35 |
Correct |
11 ms |
14828 KB |
Output is correct |
36 |
Correct |
12 ms |
15212 KB |
Output is correct |
37 |
Correct |
10 ms |
13164 KB |
Output is correct |
38 |
Correct |
9 ms |
12012 KB |
Output is correct |
39 |
Correct |
247 ms |
17644 KB |
Output is correct |
40 |
Correct |
243 ms |
17644 KB |
Output is correct |
41 |
Correct |
259 ms |
17516 KB |
Output is correct |
42 |
Correct |
241 ms |
17852 KB |
Output is correct |
43 |
Correct |
148 ms |
17900 KB |
Output is correct |
44 |
Correct |
92 ms |
17772 KB |
Output is correct |
45 |
Correct |
216 ms |
21100 KB |
Output is correct |
46 |
Correct |
133 ms |
24812 KB |
Output is correct |
47 |
Correct |
236 ms |
25708 KB |
Output is correct |
48 |
Correct |
587 ms |
40172 KB |
Output is correct |
49 |
Correct |
577 ms |
39020 KB |
Output is correct |
50 |
Correct |
249 ms |
25964 KB |
Output is correct |
51 |
Correct |
242 ms |
25708 KB |
Output is correct |
52 |
Correct |
359 ms |
25836 KB |
Output is correct |
53 |
Correct |
443 ms |
24812 KB |
Output is correct |
54 |
Correct |
397 ms |
25452 KB |
Output is correct |
55 |
Correct |
17 ms |
10604 KB |
Output is correct |
56 |
Correct |
19 ms |
10604 KB |
Output is correct |
57 |
Correct |
116 ms |
17804 KB |
Output is correct |
58 |
Correct |
48 ms |
17504 KB |
Output is correct |
59 |
Correct |
134 ms |
25580 KB |
Output is correct |
60 |
Correct |
872 ms |
44140 KB |
Output is correct |
61 |
Correct |
255 ms |
25964 KB |
Output is correct |
62 |
Correct |
283 ms |
25964 KB |
Output is correct |
63 |
Correct |
425 ms |
25836 KB |
Output is correct |
64 |
Correct |
914 ms |
28140 KB |
Output is correct |
65 |
Correct |
492 ms |
26660 KB |
Output is correct |
66 |
Correct |
766 ms |
44524 KB |
Output is correct |
67 |
Correct |
163 ms |
26464 KB |
Output is correct |
68 |
Correct |
440 ms |
31980 KB |
Output is correct |
69 |
Correct |
441 ms |
32108 KB |
Output is correct |
70 |
Correct |
416 ms |
31084 KB |
Output is correct |