#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
const int mxNN = 2e5+1000;
const int mxN = 1e6 + 500;
const int INF = (int) 1e9;
vector<pair<int,int>> adj[mxNN];
int sz[mxNN],dp[mxN],K,ans=INF;
bool ok[mxNN];
void dfs(int node,int par){
sz[node]=1;
for(auto it:adj[node]){
int adj=it.first;
if(par!=adj&&!ok[adj]){
dfs(adj,node);
sz[node]+=sz[adj];
}
}
}
int find(int node,int par,int n){
for(auto it:adj[node]){
int adj=it.first;
if(par!=adj&&!ok[adj]&&sz[it.first]*2>n){
return find(it.first,node,n);
}
}
return node;
}
void path_finder(int u,int par,int h,int tot){
if(tot>K) return;
ans=min(ans,dp[K-tot]+h);
for(auto it:adj[u]){
if(it.first!=par&&!ok[it.first]){
if(tot+it.second>K) continue;
path_finder(it.first,u,h+1,tot+it.second);
}
}
}
void path_optimize(int u,int par,int h,int tot,unordered_set<int> &st){
if(tot>K) return;
st.insert(tot);
dp[tot]=min(dp[tot],h);
for(auto it:adj[u]){
if(it.first!=par&&!ok[it.first]){
if(tot+it.second>K) continue;
path_optimize(it.first,u,h+1,tot+it.second,st);
}
}
}
void decompose(int u){
dfs(u,0);
int cen=find(u,0,sz[u]);
dp[0]=0;
unordered_set<int> st;
for(auto it:adj[cen]){
if(!ok[it.first]){
path_finder(it.first,cen,1,it.second);
path_optimize(it.first,cen,1,it.second,st);
}
}
for(auto it:st) dp[it]=INF;
ok[cen]=1;
for(auto it:adj[cen]){
if(!ok[it.first]){
decompose(it.first);
}
}
}
int best_path(int n,int k,int h[][2],int l[]){
for(int i=0;i<n-1;++i){
adj[h[i][0]].emplace_back(h[i][1],l[i]);
adj[h[i][1]].emplace_back(h[i][0],l[i]);
}
memset(dp,0x3f,sizeof(dp));
K=k;
decompose(0);
return ans>=INF?-1:ans;
}
Compilation message
race.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("O3")
|
race.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9048 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
4 ms |
9052 KB |
Output is correct |
4 |
Correct |
4 ms |
9104 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9052 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
6 ms |
9052 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
4 ms |
9008 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9048 KB |
Output is correct |
14 |
Correct |
4 ms |
9048 KB |
Output is correct |
15 |
Correct |
3 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9316 KB |
Output is correct |
18 |
Correct |
4 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9048 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
4 ms |
9052 KB |
Output is correct |
4 |
Correct |
4 ms |
9104 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9052 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
6 ms |
9052 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
4 ms |
9008 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9048 KB |
Output is correct |
14 |
Correct |
4 ms |
9048 KB |
Output is correct |
15 |
Correct |
3 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9316 KB |
Output is correct |
18 |
Correct |
4 ms |
9048 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
4 ms |
9052 KB |
Output is correct |
21 |
Correct |
5 ms |
9052 KB |
Output is correct |
22 |
Correct |
4 ms |
9052 KB |
Output is correct |
23 |
Correct |
4 ms |
9020 KB |
Output is correct |
24 |
Correct |
4 ms |
9048 KB |
Output is correct |
25 |
Correct |
4 ms |
9052 KB |
Output is correct |
26 |
Correct |
4 ms |
9052 KB |
Output is correct |
27 |
Correct |
5 ms |
9052 KB |
Output is correct |
28 |
Correct |
5 ms |
9052 KB |
Output is correct |
29 |
Correct |
5 ms |
9048 KB |
Output is correct |
30 |
Correct |
5 ms |
9052 KB |
Output is correct |
31 |
Correct |
5 ms |
9232 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
5 ms |
9040 KB |
Output is correct |
34 |
Correct |
4 ms |
9052 KB |
Output is correct |
35 |
Correct |
7 ms |
9052 KB |
Output is correct |
36 |
Correct |
4 ms |
9052 KB |
Output is correct |
37 |
Correct |
5 ms |
9124 KB |
Output is correct |
38 |
Correct |
5 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9048 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
4 ms |
9052 KB |
Output is correct |
4 |
Correct |
4 ms |
9104 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9052 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
6 ms |
9052 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
4 ms |
9008 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9048 KB |
Output is correct |
14 |
Correct |
4 ms |
9048 KB |
Output is correct |
15 |
Correct |
3 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9316 KB |
Output is correct |
18 |
Correct |
4 ms |
9048 KB |
Output is correct |
19 |
Correct |
92 ms |
15704 KB |
Output is correct |
20 |
Correct |
96 ms |
15708 KB |
Output is correct |
21 |
Correct |
93 ms |
15700 KB |
Output is correct |
22 |
Correct |
87 ms |
15708 KB |
Output is correct |
23 |
Correct |
50 ms |
15932 KB |
Output is correct |
24 |
Correct |
49 ms |
15952 KB |
Output is correct |
25 |
Correct |
91 ms |
19284 KB |
Output is correct |
26 |
Correct |
65 ms |
22864 KB |
Output is correct |
27 |
Correct |
111 ms |
22848 KB |
Output is correct |
28 |
Correct |
161 ms |
37548 KB |
Output is correct |
29 |
Correct |
160 ms |
36168 KB |
Output is correct |
30 |
Correct |
110 ms |
22796 KB |
Output is correct |
31 |
Correct |
115 ms |
22812 KB |
Output is correct |
32 |
Correct |
118 ms |
22808 KB |
Output is correct |
33 |
Correct |
156 ms |
21532 KB |
Output is correct |
34 |
Correct |
119 ms |
22608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9048 KB |
Output is correct |
2 |
Correct |
3 ms |
9052 KB |
Output is correct |
3 |
Correct |
4 ms |
9052 KB |
Output is correct |
4 |
Correct |
4 ms |
9104 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9052 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
6 ms |
9052 KB |
Output is correct |
10 |
Correct |
3 ms |
9052 KB |
Output is correct |
11 |
Correct |
4 ms |
9008 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9048 KB |
Output is correct |
14 |
Correct |
4 ms |
9048 KB |
Output is correct |
15 |
Correct |
3 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
9316 KB |
Output is correct |
18 |
Correct |
4 ms |
9048 KB |
Output is correct |
19 |
Correct |
4 ms |
9052 KB |
Output is correct |
20 |
Correct |
4 ms |
9052 KB |
Output is correct |
21 |
Correct |
5 ms |
9052 KB |
Output is correct |
22 |
Correct |
4 ms |
9052 KB |
Output is correct |
23 |
Correct |
4 ms |
9020 KB |
Output is correct |
24 |
Correct |
4 ms |
9048 KB |
Output is correct |
25 |
Correct |
4 ms |
9052 KB |
Output is correct |
26 |
Correct |
4 ms |
9052 KB |
Output is correct |
27 |
Correct |
5 ms |
9052 KB |
Output is correct |
28 |
Correct |
5 ms |
9052 KB |
Output is correct |
29 |
Correct |
5 ms |
9048 KB |
Output is correct |
30 |
Correct |
5 ms |
9052 KB |
Output is correct |
31 |
Correct |
5 ms |
9232 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
5 ms |
9040 KB |
Output is correct |
34 |
Correct |
4 ms |
9052 KB |
Output is correct |
35 |
Correct |
7 ms |
9052 KB |
Output is correct |
36 |
Correct |
4 ms |
9052 KB |
Output is correct |
37 |
Correct |
5 ms |
9124 KB |
Output is correct |
38 |
Correct |
5 ms |
9052 KB |
Output is correct |
39 |
Correct |
92 ms |
15704 KB |
Output is correct |
40 |
Correct |
96 ms |
15708 KB |
Output is correct |
41 |
Correct |
93 ms |
15700 KB |
Output is correct |
42 |
Correct |
87 ms |
15708 KB |
Output is correct |
43 |
Correct |
50 ms |
15932 KB |
Output is correct |
44 |
Correct |
49 ms |
15952 KB |
Output is correct |
45 |
Correct |
91 ms |
19284 KB |
Output is correct |
46 |
Correct |
65 ms |
22864 KB |
Output is correct |
47 |
Correct |
111 ms |
22848 KB |
Output is correct |
48 |
Correct |
161 ms |
37548 KB |
Output is correct |
49 |
Correct |
160 ms |
36168 KB |
Output is correct |
50 |
Correct |
110 ms |
22796 KB |
Output is correct |
51 |
Correct |
115 ms |
22812 KB |
Output is correct |
52 |
Correct |
118 ms |
22808 KB |
Output is correct |
53 |
Correct |
156 ms |
21532 KB |
Output is correct |
54 |
Correct |
119 ms |
22608 KB |
Output is correct |
55 |
Correct |
13 ms |
10072 KB |
Output is correct |
56 |
Correct |
11 ms |
9764 KB |
Output is correct |
57 |
Correct |
61 ms |
15960 KB |
Output is correct |
58 |
Correct |
24 ms |
15560 KB |
Output is correct |
59 |
Correct |
125 ms |
29028 KB |
Output is correct |
60 |
Correct |
467 ms |
53304 KB |
Output is correct |
61 |
Correct |
149 ms |
23192 KB |
Output is correct |
62 |
Correct |
152 ms |
23124 KB |
Output is correct |
63 |
Correct |
179 ms |
23012 KB |
Output is correct |
64 |
Correct |
412 ms |
33064 KB |
Output is correct |
65 |
Correct |
107 ms |
23636 KB |
Output is correct |
66 |
Correct |
352 ms |
34896 KB |
Output is correct |
67 |
Correct |
60 ms |
23416 KB |
Output is correct |
68 |
Correct |
204 ms |
29572 KB |
Output is correct |
69 |
Correct |
187 ms |
29608 KB |
Output is correct |
70 |
Correct |
196 ms |
28772 KB |
Output is correct |