#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define getar(ar,n) for(ll i=0;i<n;++i) cin>>ar[i]
#define show(n) cout<<n<<'\n'
#define all(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 mxN = 1e6 + 50;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
vector<pair<ll,ll>> adj[mxN];
vector<ll> dp(mxN,INF);
ll sz[mxN],N,K,ans=INF;
bool ok[mxN];
void dfs(ll node,ll 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(ll node,ll 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(ll u,ll par,ll h,ll 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(ll u,ll par,ll h,ll 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(ll u,ll par,ll 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;
}
/*
int main(){
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
int n,k;cin>>n>>k;
int h[n-1][2],l[n-1];
for(int i=0;i<n-1;++i){
cin>>h[i][0]>>h[i][1];
}
for(int i=0;i<n-1;++i){
cin>>l[i];
}
cout<<best_path(n,k,h,l);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
14 ms |
31580 KB |
Output is correct |
3 |
Correct |
13 ms |
31580 KB |
Output is correct |
4 |
Correct |
17 ms |
31832 KB |
Output is correct |
5 |
Correct |
14 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31580 KB |
Output is correct |
7 |
Correct |
14 ms |
31580 KB |
Output is correct |
8 |
Correct |
14 ms |
31580 KB |
Output is correct |
9 |
Correct |
14 ms |
31800 KB |
Output is correct |
10 |
Correct |
14 ms |
31580 KB |
Output is correct |
11 |
Correct |
13 ms |
31788 KB |
Output is correct |
12 |
Correct |
14 ms |
31580 KB |
Output is correct |
13 |
Correct |
13 ms |
31716 KB |
Output is correct |
14 |
Correct |
14 ms |
31580 KB |
Output is correct |
15 |
Correct |
15 ms |
31580 KB |
Output is correct |
16 |
Correct |
14 ms |
31756 KB |
Output is correct |
17 |
Correct |
14 ms |
31768 KB |
Output is correct |
18 |
Correct |
17 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
14 ms |
31580 KB |
Output is correct |
3 |
Correct |
13 ms |
31580 KB |
Output is correct |
4 |
Correct |
17 ms |
31832 KB |
Output is correct |
5 |
Correct |
14 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31580 KB |
Output is correct |
7 |
Correct |
14 ms |
31580 KB |
Output is correct |
8 |
Correct |
14 ms |
31580 KB |
Output is correct |
9 |
Correct |
14 ms |
31800 KB |
Output is correct |
10 |
Correct |
14 ms |
31580 KB |
Output is correct |
11 |
Correct |
13 ms |
31788 KB |
Output is correct |
12 |
Correct |
14 ms |
31580 KB |
Output is correct |
13 |
Correct |
13 ms |
31716 KB |
Output is correct |
14 |
Correct |
14 ms |
31580 KB |
Output is correct |
15 |
Correct |
15 ms |
31580 KB |
Output is correct |
16 |
Correct |
14 ms |
31756 KB |
Output is correct |
17 |
Correct |
14 ms |
31768 KB |
Output is correct |
18 |
Correct |
17 ms |
31580 KB |
Output is correct |
19 |
Correct |
14 ms |
31580 KB |
Output is correct |
20 |
Correct |
13 ms |
31728 KB |
Output is correct |
21 |
Correct |
14 ms |
31780 KB |
Output is correct |
22 |
Correct |
14 ms |
32088 KB |
Output is correct |
23 |
Correct |
15 ms |
31836 KB |
Output is correct |
24 |
Correct |
14 ms |
31644 KB |
Output is correct |
25 |
Correct |
13 ms |
31836 KB |
Output is correct |
26 |
Correct |
14 ms |
31680 KB |
Output is correct |
27 |
Correct |
14 ms |
31832 KB |
Output is correct |
28 |
Correct |
14 ms |
31836 KB |
Output is correct |
29 |
Correct |
14 ms |
31832 KB |
Output is correct |
30 |
Correct |
18 ms |
31780 KB |
Output is correct |
31 |
Correct |
14 ms |
31836 KB |
Output is correct |
32 |
Correct |
14 ms |
31856 KB |
Output is correct |
33 |
Correct |
15 ms |
31832 KB |
Output is correct |
34 |
Correct |
15 ms |
31836 KB |
Output is correct |
35 |
Correct |
16 ms |
31968 KB |
Output is correct |
36 |
Correct |
16 ms |
31836 KB |
Output is correct |
37 |
Correct |
19 ms |
31836 KB |
Output is correct |
38 |
Correct |
16 ms |
31832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
14 ms |
31580 KB |
Output is correct |
3 |
Correct |
13 ms |
31580 KB |
Output is correct |
4 |
Correct |
17 ms |
31832 KB |
Output is correct |
5 |
Correct |
14 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31580 KB |
Output is correct |
7 |
Correct |
14 ms |
31580 KB |
Output is correct |
8 |
Correct |
14 ms |
31580 KB |
Output is correct |
9 |
Correct |
14 ms |
31800 KB |
Output is correct |
10 |
Correct |
14 ms |
31580 KB |
Output is correct |
11 |
Correct |
13 ms |
31788 KB |
Output is correct |
12 |
Correct |
14 ms |
31580 KB |
Output is correct |
13 |
Correct |
13 ms |
31716 KB |
Output is correct |
14 |
Correct |
14 ms |
31580 KB |
Output is correct |
15 |
Correct |
15 ms |
31580 KB |
Output is correct |
16 |
Correct |
14 ms |
31756 KB |
Output is correct |
17 |
Correct |
14 ms |
31768 KB |
Output is correct |
18 |
Correct |
17 ms |
31580 KB |
Output is correct |
19 |
Correct |
192 ms |
40020 KB |
Output is correct |
20 |
Correct |
185 ms |
40272 KB |
Output is correct |
21 |
Correct |
176 ms |
40268 KB |
Output is correct |
22 |
Correct |
113 ms |
40276 KB |
Output is correct |
23 |
Correct |
66 ms |
40784 KB |
Output is correct |
24 |
Correct |
50 ms |
39792 KB |
Output is correct |
25 |
Execution timed out |
3007 ms |
43088 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
14 ms |
31580 KB |
Output is correct |
3 |
Correct |
13 ms |
31580 KB |
Output is correct |
4 |
Correct |
17 ms |
31832 KB |
Output is correct |
5 |
Correct |
14 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31580 KB |
Output is correct |
7 |
Correct |
14 ms |
31580 KB |
Output is correct |
8 |
Correct |
14 ms |
31580 KB |
Output is correct |
9 |
Correct |
14 ms |
31800 KB |
Output is correct |
10 |
Correct |
14 ms |
31580 KB |
Output is correct |
11 |
Correct |
13 ms |
31788 KB |
Output is correct |
12 |
Correct |
14 ms |
31580 KB |
Output is correct |
13 |
Correct |
13 ms |
31716 KB |
Output is correct |
14 |
Correct |
14 ms |
31580 KB |
Output is correct |
15 |
Correct |
15 ms |
31580 KB |
Output is correct |
16 |
Correct |
14 ms |
31756 KB |
Output is correct |
17 |
Correct |
14 ms |
31768 KB |
Output is correct |
18 |
Correct |
17 ms |
31580 KB |
Output is correct |
19 |
Correct |
14 ms |
31580 KB |
Output is correct |
20 |
Correct |
13 ms |
31728 KB |
Output is correct |
21 |
Correct |
14 ms |
31780 KB |
Output is correct |
22 |
Correct |
14 ms |
32088 KB |
Output is correct |
23 |
Correct |
15 ms |
31836 KB |
Output is correct |
24 |
Correct |
14 ms |
31644 KB |
Output is correct |
25 |
Correct |
13 ms |
31836 KB |
Output is correct |
26 |
Correct |
14 ms |
31680 KB |
Output is correct |
27 |
Correct |
14 ms |
31832 KB |
Output is correct |
28 |
Correct |
14 ms |
31836 KB |
Output is correct |
29 |
Correct |
14 ms |
31832 KB |
Output is correct |
30 |
Correct |
18 ms |
31780 KB |
Output is correct |
31 |
Correct |
14 ms |
31836 KB |
Output is correct |
32 |
Correct |
14 ms |
31856 KB |
Output is correct |
33 |
Correct |
15 ms |
31832 KB |
Output is correct |
34 |
Correct |
15 ms |
31836 KB |
Output is correct |
35 |
Correct |
16 ms |
31968 KB |
Output is correct |
36 |
Correct |
16 ms |
31836 KB |
Output is correct |
37 |
Correct |
19 ms |
31836 KB |
Output is correct |
38 |
Correct |
16 ms |
31832 KB |
Output is correct |
39 |
Correct |
192 ms |
40020 KB |
Output is correct |
40 |
Correct |
185 ms |
40272 KB |
Output is correct |
41 |
Correct |
176 ms |
40268 KB |
Output is correct |
42 |
Correct |
113 ms |
40276 KB |
Output is correct |
43 |
Correct |
66 ms |
40784 KB |
Output is correct |
44 |
Correct |
50 ms |
39792 KB |
Output is correct |
45 |
Execution timed out |
3007 ms |
43088 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |