Submission #486547

#TimeUsernameProblemLanguageResultExecution timeMemory
486547julian33Untitled (POI11_dyn)C++14
100 / 100
1517 ms39064 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__) template<typename ...Args> void logger(string vars, Args&&... values) { cerr<<vars<<" = "; string delim=""; (...,(cerr<<delim<<values,delim=", ")); cerr<<"\n"; } #else #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__) template<typename ...Args> void logger(string vars, Args&&... values) {} #endif #define pb push_back #define sz(x) (int)(x.size()) typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; template<typename T> inline void maxa(T& a,T b){a=max(a,b);} template<typename T> inline void mina(T& a,T b){a=min(a,b);} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int mxN=3e5+5; //make sure this is right const int mod=1e9+7; vector<int> graph[mxN]; pii dp[mxN]; int d[mxN],dist[mxN]; void dfs(int at,int p,int k){ dp[at].second=d[at]?0:-1; for(int &i:graph[at]){ if(i==p) continue; dfs(i,at,k); mina(dist[at],dist[i]+1); } if(dist[at]<=k) dp[at].second=-1; for(int &i:graph[at]){ if(i==p) continue; dp[at].first+=dp[i].first; if(dp[i].second==-1) continue; if(dp[i].second+1+dist[at]>k){ if(dp[i].second+1==k) dp[at].first++,dist[at]=0; else maxa(dp[at].second,dp[i].second+1); } } if(dist[at]+dp[at].second<=k) dp[at].second=-1; } bool check(int k,int n,int m){ for(int i=1;i<=n;i++){ dp[i]={0,0}; dist[i]=1e9; } dfs(1,-1,k); int res=dp[1].first+(~dp[1].second?1:0); return res<=m; } int main(){ cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int n,m; cin>>n>>m; int tot=0; for(int i=1;i<=n;i++){ cin>>d[i]; tot+=d[i]; } for(int i=0;i<n-1;i++){ int a,b; cin>>a>>b; graph[a].pb(b); graph[b].pb(a); } if(tot<=m){ cout<<0<<"\n"; return 0; } int l=1; int r=n; int ans=n; while(l<=r){ int mid=(l+r)>>1; if(check(mid,n,m)){ ans=mid; r=mid-1; } else{ l=mid+1; } } cout<<ans<<"\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...