#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adj;
int n, k;
vector<int> subtr;
vector<bool> vis;
unordered_map<int,int> mp1;
unordered_map<int,int>mp2;
int ans=INT_MAX;
int cnt=0;
void findsize(int u, int p){
subtr[u]=1;
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v]){
findsize(v,u);
subtr[u]+=subtr[v];
}
}
}
int findcentroid(int u, int p, int sz){
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v] and subtr[v]>sz/2)return findcentroid(v,u,sz);
}
return u;
}
void dfs1(int u, int p, int dist, int depth){
if(dist>k)return;
if((mp1[k-dist]!=0 and mp2[k-dist]==cnt) or k==dist) ans=min(ans,mp1[k-dist]+depth);
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v]){
dfs1(v,u,dist+w,depth+1);
}
}
}
void dfs2(int u, int p, int dist, int depth){
if(dist>k)return;
if(mp1[dist]==0){
mp1[dist]=depth;
mp2[dist]=cnt;
}
else{
if(mp2[dist]<cnt){
mp1[dist]=depth;
mp2[dist]=cnt;
}
else if(mp1[dist]>depth){
mp1[dist]=depth;
mp2[dist]=cnt;
}
}
for(auto [v,w]:adj[u]){
if(v!=p and !vis[v]){
dfs2(v,u,dist+w,depth+1);
}
}
}
void buildcentroid(int u, int p){
cnt++;
findsize(u,u);
int c=findcentroid(u,u,subtr[u]);
vis[c]=true;
mp2[0]=cnt;
for(auto [v,w]:adj[c]){
if(!vis[v]){
dfs1(v,c,w,1);
dfs2(v,c,w,1);
}
}
for(auto [v,w]:adj[c]){
if(!vis[v])buildcentroid(v,c);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;
k=K;
adj.resize(n);
subtr.resize(n);
vis.resize(n);
for(int i=0;i<n-1;i++){
adj[H[i][1]].push_back({H[i][0],L[i]});
adj[H[i][0]].push_back({H[i][1],L[i]});
}
buildcentroid(0,-1);
if(ans==INT_MAX)return -1;
return ans;
}
Compilation message
race.cpp: In function 'void findsize(int, int)':
race.cpp:16:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for(auto [v,w]:adj[u]){
| ^
race.cpp: In function 'int findcentroid(int, int, int)':
race.cpp:25:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
25 | for(auto [v,w]:adj[u]){
| ^
race.cpp: In function 'void dfs1(int, int, int, int)':
race.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [v,w]:adj[u]){
| ^
race.cpp: In function 'void dfs2(int, int, int, int)':
race.cpp:57:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for(auto [v,w]:adj[u]){
| ^
race.cpp: In function 'void buildcentroid(int, int)':
race.cpp:70:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
70 | for(auto [v,w]:adj[c]){
| ^
race.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | for(auto [v,w]:adj[c]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2748 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2760 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
3008 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
3008 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2916 KB |
Output is correct |
37 |
Correct |
4 ms |
3352 KB |
Output is correct |
38 |
Correct |
3 ms |
3256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
100 ms |
11176 KB |
Output is correct |
20 |
Correct |
94 ms |
12112 KB |
Output is correct |
21 |
Correct |
113 ms |
11880 KB |
Output is correct |
22 |
Correct |
135 ms |
12556 KB |
Output is correct |
23 |
Correct |
50 ms |
12636 KB |
Output is correct |
24 |
Correct |
43 ms |
12380 KB |
Output is correct |
25 |
Correct |
108 ms |
14972 KB |
Output is correct |
26 |
Correct |
94 ms |
17604 KB |
Output is correct |
27 |
Correct |
126 ms |
19088 KB |
Output is correct |
28 |
Correct |
191 ms |
30780 KB |
Output is correct |
29 |
Correct |
182 ms |
30032 KB |
Output is correct |
30 |
Correct |
117 ms |
19700 KB |
Output is correct |
31 |
Correct |
129 ms |
20164 KB |
Output is correct |
32 |
Correct |
127 ms |
19660 KB |
Output is correct |
33 |
Correct |
162 ms |
18772 KB |
Output is correct |
34 |
Correct |
111 ms |
19160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2748 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2760 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
3008 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
3008 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3420 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2916 KB |
Output is correct |
37 |
Correct |
4 ms |
3352 KB |
Output is correct |
38 |
Correct |
3 ms |
3256 KB |
Output is correct |
39 |
Correct |
100 ms |
11176 KB |
Output is correct |
40 |
Correct |
94 ms |
12112 KB |
Output is correct |
41 |
Correct |
113 ms |
11880 KB |
Output is correct |
42 |
Correct |
135 ms |
12556 KB |
Output is correct |
43 |
Correct |
50 ms |
12636 KB |
Output is correct |
44 |
Correct |
43 ms |
12380 KB |
Output is correct |
45 |
Correct |
108 ms |
14972 KB |
Output is correct |
46 |
Correct |
94 ms |
17604 KB |
Output is correct |
47 |
Correct |
126 ms |
19088 KB |
Output is correct |
48 |
Correct |
191 ms |
30780 KB |
Output is correct |
49 |
Correct |
182 ms |
30032 KB |
Output is correct |
50 |
Correct |
117 ms |
19700 KB |
Output is correct |
51 |
Correct |
129 ms |
20164 KB |
Output is correct |
52 |
Correct |
127 ms |
19660 KB |
Output is correct |
53 |
Correct |
162 ms |
18772 KB |
Output is correct |
54 |
Correct |
111 ms |
19160 KB |
Output is correct |
55 |
Correct |
9 ms |
3420 KB |
Output is correct |
56 |
Correct |
9 ms |
3160 KB |
Output is correct |
57 |
Correct |
86 ms |
11376 KB |
Output is correct |
58 |
Correct |
29 ms |
12232 KB |
Output is correct |
59 |
Correct |
153 ms |
28916 KB |
Output is correct |
60 |
Correct |
871 ms |
69680 KB |
Output is correct |
61 |
Correct |
166 ms |
19484 KB |
Output is correct |
62 |
Correct |
175 ms |
20300 KB |
Output is correct |
63 |
Correct |
208 ms |
20048 KB |
Output is correct |
64 |
Correct |
1094 ms |
47024 KB |
Output is correct |
65 |
Correct |
124 ms |
20816 KB |
Output is correct |
66 |
Correct |
619 ms |
42980 KB |
Output is correct |
67 |
Correct |
74 ms |
21416 KB |
Output is correct |
68 |
Correct |
514 ms |
48344 KB |
Output is correct |
69 |
Correct |
509 ms |
46232 KB |
Output is correct |
70 |
Correct |
548 ms |
47504 KB |
Output is correct |