Submission #504036

# Submission time Handle Problem Language Result Execution time Memory
504036 2022-01-09T14:45:30 Z tgbegimx Dynamite (POI11_dyn) C++17
100 / 100
1350 ms 37892 KB
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0) ; cin.tie(0);cout.tie(0)
#define ll long long
#define foru(i,l,r) for(int i=l;i<=r;i++)
#define ford(i,l,r) for(int i=l;i>=r;i--)
#define pb push_back
using namespace std;
const int N=3e5+10;
vector<int> d;
vector<vector<int>> ke;
int dp[N],dem,kt[N];
int n,m;
void dfs(int k,int u,int fa)
{
    int mx1=-1e9,mx2=-1e9;
    if(d[u]) mx1=0;
    for(auto v:ke[u])
    {
        if(v==fa) continue;
        //cout<<v<<' ';
        dfs(k,v,u);
        if(kt[v]==1) mx1=max(mx1,dp[v]+1);
        if(kt[v]==2) mx2=max(mx2,dp[v]-1);
    }
    if(mx1>mx2)
    {
        if(mx1>=k)
        {
          dem++;
          kt[u]=2;
          dp[u]=k;
          if(dem>m) kt[u]=-1;
        }
        else kt[u]=1,dp[u]=mx1;
    }
    else kt[u]=2,dp[u]=mx2;

}
bool check(int k)
{
    dem=0;
    dfs(k,1,-1);
    if(kt[1]==-1) return 0;
    if(kt[1]==1&&dp[1]>=0) dem++;
    if(dem<=m) return 1;
    return 0;
}
void solve()
{
    cin>>n>>m;
    d.resize(n+1);
    ke.resize(n+1);
    foru(i,1,n) cin>>d[i];
    foru(i,1,n-1)
    {
        int u,v;
        cin>>u>>v;
        ke[u].pb(v);
        ke[v].pb(u);
    }
    //for(auto v:ke[5]) cout<<v<<' ';
    int l=0,r=n,ans=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        //cout<<check(mid)<<' ';
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    cout<<ans;
}
int main()
{
    //freopen("main.inp","r",stdin);
    //freopen("main.out","w",stdout);
    fast;
    int t=1;
    //cin>>t;
    while(t--)
       solve();

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1100 KB Output is correct
2 Correct 25 ms 2136 KB Output is correct
3 Correct 26 ms 2632 KB Output is correct
4 Correct 40 ms 4684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4404 KB Output is correct
2 Correct 67 ms 5700 KB Output is correct
3 Correct 118 ms 6184 KB Output is correct
4 Correct 157 ms 9704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 7624 KB Output is correct
2 Correct 147 ms 7816 KB Output is correct
3 Correct 198 ms 7516 KB Output is correct
4 Correct 376 ms 13788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 552 ms 14536 KB Output is correct
2 Correct 627 ms 16920 KB Output is correct
3 Correct 1032 ms 29592 KB Output is correct
4 Correct 1048 ms 29568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1258 ms 23960 KB Output is correct
2 Correct 1036 ms 25672 KB Output is correct
3 Correct 1350 ms 31268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1206 ms 31528 KB Output is correct
2 Correct 985 ms 25660 KB Output is correct
3 Correct 1340 ms 37892 KB Output is correct
4 Correct 331 ms 26028 KB Output is correct