Submission #504023

# Submission time Handle Problem Language Result Execution time Memory
504023 2022-01-09T14:22:42 Z tgbegimx Dynamite (POI11_dyn) C++17
100 / 100
1292 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 f[N],num,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)
    {
        //cout<<v<<' ';
        dfs(k,v,u);
        if(kt[v]==1) mx1=max(mx1,f[v]+1);
        if(kt[v]==2) mx2=max(mx2,f[v]-1);
    }
    if(mx1>mx2)
    {
        if(mx1>=k)
        {
          ++num,kt[u]=2,f[u]=k;
          if(num>m) kt[u]=-1;
        }
        else kt[u]=1,f[u]=mx1;
    }
    else kt[u]=2,f[u]=mx2;

}
bool check(int k)
{
    num=0;
    dfs(k,1,-1);
    //cout<<'\n';
    if(kt[1]==-1) return 0;
    if(kt[1]==1&&f[1]>=0) ++num;
    return num<=m;
}
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;
    int 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 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 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 332 KB Output is correct
3 Correct 0 ms 204 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 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 984 KB Output is correct
2 Correct 22 ms 1908 KB Output is correct
3 Correct 30 ms 2296 KB Output is correct
4 Correct 32 ms 4344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 3848 KB Output is correct
2 Correct 64 ms 5216 KB Output is correct
3 Correct 103 ms 5652 KB Output is correct
4 Correct 180 ms 9084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 7204 KB Output is correct
2 Correct 167 ms 7352 KB Output is correct
3 Correct 169 ms 7012 KB Output is correct
4 Correct 239 ms 13176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 542 ms 13956 KB Output is correct
2 Correct 599 ms 16344 KB Output is correct
3 Correct 996 ms 29604 KB Output is correct
4 Correct 1084 ms 29700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1282 ms 23444 KB Output is correct
2 Correct 926 ms 25808 KB Output is correct
3 Correct 1292 ms 31244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1077 ms 31012 KB Output is correct
2 Correct 929 ms 25676 KB Output is correct
3 Correct 1278 ms 37892 KB Output is correct
4 Correct 333 ms 25968 KB Output is correct