Submission #486547

# Submission time Handle Problem Language Result Execution time Memory
486547 2021-11-12T01:30:07 Z julian33 Dynamite (POI11_dyn) C++14
100 / 100
1517 ms 39064 KB
#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 time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 7892 KB Output is correct
2 Correct 26 ms 8792 KB Output is correct
3 Correct 29 ms 9172 KB Output is correct
4 Correct 41 ms 11236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 10572 KB Output is correct
2 Correct 76 ms 11816 KB Output is correct
3 Correct 132 ms 12140 KB Output is correct
4 Correct 147 ms 15648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 13628 KB Output is correct
2 Correct 159 ms 13740 KB Output is correct
3 Correct 206 ms 13544 KB Output is correct
4 Correct 289 ms 19672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 610 ms 19996 KB Output is correct
2 Correct 688 ms 22340 KB Output is correct
3 Correct 1147 ms 31804 KB Output is correct
4 Correct 1162 ms 31740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1488 ms 29180 KB Output is correct
2 Correct 1095 ms 26848 KB Output is correct
3 Correct 1432 ms 32456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1327 ms 36696 KB Output is correct
2 Correct 1062 ms 26908 KB Output is correct
3 Correct 1517 ms 39064 KB Output is correct
4 Correct 434 ms 27184 KB Output is correct