#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],N,K,ans=INF;
void dfs(int node,int par){
sz[node]=0;
for(auto it:adj[node]){
if(it.first==par||ok[node]) continue;
dfs(it.first,node);
sz[node]+=sz[it.first];
}
sz[node]++;
}
int find(int node,int par){
for(auto it:adj[node]){
if(it.first==par||ok[node]) continue;
if(sz[it.first]>=((N+1)/2)){
return find(it.first,node);
}
}
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);
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]});
}
N=n,K=k;
decompose(0);
return ans>=INF?-1:ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 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 |
8928 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 |
9120 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
9052 KB |
Output is correct |
11 |
Correct |
5 ms |
9052 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9148 KB |
Output is correct |
17 |
Correct |
3 ms |
9116 KB |
Output is correct |
18 |
Correct |
4 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 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 |
8928 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 |
9120 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
9052 KB |
Output is correct |
11 |
Correct |
5 ms |
9052 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9148 KB |
Output is correct |
17 |
Correct |
3 ms |
9116 KB |
Output is correct |
18 |
Correct |
4 ms |
9052 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 |
5 ms |
9052 KB |
Output is correct |
23 |
Correct |
4 ms |
9052 KB |
Output is correct |
24 |
Correct |
5 ms |
9052 KB |
Output is correct |
25 |
Correct |
5 ms |
9052 KB |
Output is correct |
26 |
Correct |
4 ms |
9052 KB |
Output is correct |
27 |
Correct |
4 ms |
9052 KB |
Output is correct |
28 |
Correct |
5 ms |
9196 KB |
Output is correct |
29 |
Correct |
5 ms |
9188 KB |
Output is correct |
30 |
Correct |
5 ms |
9052 KB |
Output is correct |
31 |
Correct |
5 ms |
9052 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
5 ms |
9052 KB |
Output is correct |
34 |
Correct |
6 ms |
9052 KB |
Output is correct |
35 |
Correct |
6 ms |
9052 KB |
Output is correct |
36 |
Correct |
7 ms |
9052 KB |
Output is correct |
37 |
Correct |
7 ms |
9052 KB |
Output is correct |
38 |
Correct |
6 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 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 |
8928 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 |
9120 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
9052 KB |
Output is correct |
11 |
Correct |
5 ms |
9052 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9148 KB |
Output is correct |
17 |
Correct |
3 ms |
9116 KB |
Output is correct |
18 |
Correct |
4 ms |
9052 KB |
Output is correct |
19 |
Correct |
169 ms |
15440 KB |
Output is correct |
20 |
Correct |
159 ms |
15448 KB |
Output is correct |
21 |
Correct |
158 ms |
15700 KB |
Output is correct |
22 |
Correct |
97 ms |
15700 KB |
Output is correct |
23 |
Correct |
53 ms |
15964 KB |
Output is correct |
24 |
Correct |
37 ms |
15696 KB |
Output is correct |
25 |
Execution timed out |
3043 ms |
20104 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 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 |
8928 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 |
9120 KB |
Output is correct |
9 |
Correct |
4 ms |
9052 KB |
Output is correct |
10 |
Correct |
4 ms |
9052 KB |
Output is correct |
11 |
Correct |
5 ms |
9052 KB |
Output is correct |
12 |
Correct |
4 ms |
9052 KB |
Output is correct |
13 |
Correct |
4 ms |
9052 KB |
Output is correct |
14 |
Correct |
4 ms |
9052 KB |
Output is correct |
15 |
Correct |
4 ms |
9052 KB |
Output is correct |
16 |
Correct |
4 ms |
9148 KB |
Output is correct |
17 |
Correct |
3 ms |
9116 KB |
Output is correct |
18 |
Correct |
4 ms |
9052 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 |
5 ms |
9052 KB |
Output is correct |
23 |
Correct |
4 ms |
9052 KB |
Output is correct |
24 |
Correct |
5 ms |
9052 KB |
Output is correct |
25 |
Correct |
5 ms |
9052 KB |
Output is correct |
26 |
Correct |
4 ms |
9052 KB |
Output is correct |
27 |
Correct |
4 ms |
9052 KB |
Output is correct |
28 |
Correct |
5 ms |
9196 KB |
Output is correct |
29 |
Correct |
5 ms |
9188 KB |
Output is correct |
30 |
Correct |
5 ms |
9052 KB |
Output is correct |
31 |
Correct |
5 ms |
9052 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
5 ms |
9052 KB |
Output is correct |
34 |
Correct |
6 ms |
9052 KB |
Output is correct |
35 |
Correct |
6 ms |
9052 KB |
Output is correct |
36 |
Correct |
7 ms |
9052 KB |
Output is correct |
37 |
Correct |
7 ms |
9052 KB |
Output is correct |
38 |
Correct |
6 ms |
9052 KB |
Output is correct |
39 |
Correct |
169 ms |
15440 KB |
Output is correct |
40 |
Correct |
159 ms |
15448 KB |
Output is correct |
41 |
Correct |
158 ms |
15700 KB |
Output is correct |
42 |
Correct |
97 ms |
15700 KB |
Output is correct |
43 |
Correct |
53 ms |
15964 KB |
Output is correct |
44 |
Correct |
37 ms |
15696 KB |
Output is correct |
45 |
Execution timed out |
3043 ms |
20104 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |