#include <bits/stdc++.h>
using namespace std;
#define getar(ar,n) for(int i=0;i<n;++i) cin>>ar[i]
#define show(n) cout<<n<<'\n'
#define aint(v) v.begin(), v.end()
#define br cout<<"\n"
#define pb push_back
#define nl '\n'
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ret return
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
const int mxNN = 2e5+1000;
const int mxN = 1e6 + 500;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ld EPS = 1e-9;
vector<pair<int,int>> adj[mxNN];
vector<int> dp(mxN,INF);
vector<bool> ok(mxNN,0);
int sz[mxNN],K,ans=INF;
void dfs(int node,int par){
sz[node]=1;
for(auto it:adj[node]){
if(it.first==par||ok[node]) continue;
dfs(it.first,node);
sz[node]+=sz[it.first];
}
}
int find(int node,int par,int n){
for(auto it:adj[node]){
if(it.first==par||ok[node]) continue;
if(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]){
path_finder(it.first,u,h+1,tot+it.second);
}
}
}
void path_optimize(int u,int par,int h,int tot){
if(tot>K) return;
dp[tot]=min(dp[tot],h);
for(auto it:adj[u]){
if(it.first!=par&&!ok[it.first]){
path_optimize(it.first,u,h+1,tot+it.second);
}
}
}
void dp_reset(int u,int par,int tot){
if(tot>K) return;
dp[tot]=INF;
for(auto it:adj[u]){
if(it.first!=par&&!ok[it.first]){
dp_reset(it.first,u,tot+it.second);
}
}
}
void decompose(int u){
dfs(u,-1);
int cen=find(u,-1,sz[u]);
dp[0]=0;
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);
}
}
dp_reset(cen,-1,0);
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]].pb({h[i][1],l[i]});
adj[h[i][1]].pb({h[i][0],l[i]});
}
K=k;
decompose(0);
return ans>=INF?-1:ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9016 KB |
Output is correct |
3 |
Correct |
4 ms |
9120 KB |
Output is correct |
4 |
Correct |
4 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
8992 KB |
Output is correct |
11 |
Correct |
4 ms |
9048 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9132 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
3 ms |
9120 KB |
Output is correct |
17 |
Correct |
4 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9016 KB |
Output is correct |
3 |
Correct |
4 ms |
9120 KB |
Output is correct |
4 |
Correct |
4 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
8992 KB |
Output is correct |
11 |
Correct |
4 ms |
9048 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9132 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
3 ms |
9120 KB |
Output is correct |
17 |
Correct |
4 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
19 |
Correct |
3 ms |
9052 KB |
Output is correct |
20 |
Correct |
4 ms |
9112 KB |
Output is correct |
21 |
Correct |
4 ms |
9048 KB |
Output is correct |
22 |
Correct |
5 ms |
8980 KB |
Output is correct |
23 |
Correct |
4 ms |
9048 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 |
4 ms |
9136 KB |
Output is correct |
28 |
Correct |
4 ms |
9052 KB |
Output is correct |
29 |
Correct |
5 ms |
9196 KB |
Output is correct |
30 |
Correct |
5 ms |
9048 KB |
Output is correct |
31 |
Correct |
4 ms |
9052 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
4 ms |
8984 KB |
Output is correct |
34 |
Correct |
4 ms |
9052 KB |
Output is correct |
35 |
Correct |
4 ms |
9052 KB |
Output is correct |
36 |
Correct |
4 ms |
9052 KB |
Output is correct |
37 |
Correct |
4 ms |
9052 KB |
Output is correct |
38 |
Correct |
4 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9016 KB |
Output is correct |
3 |
Correct |
4 ms |
9120 KB |
Output is correct |
4 |
Correct |
4 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
8992 KB |
Output is correct |
11 |
Correct |
4 ms |
9048 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9132 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
3 ms |
9120 KB |
Output is correct |
17 |
Correct |
4 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
19 |
Correct |
86 ms |
15500 KB |
Output is correct |
20 |
Correct |
88 ms |
15444 KB |
Output is correct |
21 |
Correct |
93 ms |
15708 KB |
Output is correct |
22 |
Correct |
82 ms |
15692 KB |
Output is correct |
23 |
Correct |
49 ms |
15960 KB |
Output is correct |
24 |
Correct |
37 ms |
15708 KB |
Output is correct |
25 |
Correct |
95 ms |
19928 KB |
Output is correct |
26 |
Correct |
82 ms |
24400 KB |
Output is correct |
27 |
Correct |
123 ms |
22708 KB |
Output is correct |
28 |
Correct |
159 ms |
40272 KB |
Output is correct |
29 |
Correct |
163 ms |
38996 KB |
Output is correct |
30 |
Correct |
122 ms |
22608 KB |
Output is correct |
31 |
Correct |
121 ms |
22488 KB |
Output is correct |
32 |
Correct |
135 ms |
22608 KB |
Output is correct |
33 |
Correct |
139 ms |
21612 KB |
Output is correct |
34 |
Correct |
114 ms |
22284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
3 ms |
9016 KB |
Output is correct |
3 |
Correct |
4 ms |
9120 KB |
Output is correct |
4 |
Correct |
4 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
9048 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
4 ms |
9052 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
8992 KB |
Output is correct |
11 |
Correct |
4 ms |
9048 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9132 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
3 ms |
9120 KB |
Output is correct |
17 |
Correct |
4 ms |
9052 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
19 |
Correct |
3 ms |
9052 KB |
Output is correct |
20 |
Correct |
4 ms |
9112 KB |
Output is correct |
21 |
Correct |
4 ms |
9048 KB |
Output is correct |
22 |
Correct |
5 ms |
8980 KB |
Output is correct |
23 |
Correct |
4 ms |
9048 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 |
4 ms |
9136 KB |
Output is correct |
28 |
Correct |
4 ms |
9052 KB |
Output is correct |
29 |
Correct |
5 ms |
9196 KB |
Output is correct |
30 |
Correct |
5 ms |
9048 KB |
Output is correct |
31 |
Correct |
4 ms |
9052 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
4 ms |
8984 KB |
Output is correct |
34 |
Correct |
4 ms |
9052 KB |
Output is correct |
35 |
Correct |
4 ms |
9052 KB |
Output is correct |
36 |
Correct |
4 ms |
9052 KB |
Output is correct |
37 |
Correct |
4 ms |
9052 KB |
Output is correct |
38 |
Correct |
4 ms |
9048 KB |
Output is correct |
39 |
Correct |
86 ms |
15500 KB |
Output is correct |
40 |
Correct |
88 ms |
15444 KB |
Output is correct |
41 |
Correct |
93 ms |
15708 KB |
Output is correct |
42 |
Correct |
82 ms |
15692 KB |
Output is correct |
43 |
Correct |
49 ms |
15960 KB |
Output is correct |
44 |
Correct |
37 ms |
15708 KB |
Output is correct |
45 |
Correct |
95 ms |
19928 KB |
Output is correct |
46 |
Correct |
82 ms |
24400 KB |
Output is correct |
47 |
Correct |
123 ms |
22708 KB |
Output is correct |
48 |
Correct |
159 ms |
40272 KB |
Output is correct |
49 |
Correct |
163 ms |
38996 KB |
Output is correct |
50 |
Correct |
122 ms |
22608 KB |
Output is correct |
51 |
Correct |
121 ms |
22488 KB |
Output is correct |
52 |
Correct |
135 ms |
22608 KB |
Output is correct |
53 |
Correct |
139 ms |
21612 KB |
Output is correct |
54 |
Correct |
114 ms |
22284 KB |
Output is correct |
55 |
Correct |
9 ms |
9564 KB |
Output is correct |
56 |
Correct |
15 ms |
9636 KB |
Output is correct |
57 |
Correct |
52 ms |
15708 KB |
Output is correct |
58 |
Execution timed out |
3063 ms |
15480 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |