Submission #504074

# Submission time Handle Problem Language Result Execution time Memory
504074 2022-01-09T16:18:30 Z tgbegimx Dynamite (POI11_dyn) C++17
100 / 100
1322 ms 35524 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;
int read()
{
    int s=0,f=1;
    char ch=getchar();
    for(;ch<'0'||ch>'9';f=(ch=='-')?-1:f,ch=getchar());
    for(;ch>='0'&&ch<='9';s=s*10+(ch^48),ch=getchar());
    return s*f;
}
void dfs(int k,int u,int fa)
{
    int mx1=-1e9,mx2=-1e9;
    if(d[u]) mx1=0;
    //if(k>m) return;
    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(kt[v]==-1||dem>m) {kt[u]==-1;return;}
    }
    if(mx1>mx2)
    {
        if(mx1>=k)
        {
          ++dem;
          kt[u]=2;
          dp[u]=k;
          if(dem>m) {kt[u]=-1;return;}
        }
        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;
    return dem<=m;
}
void solve()
{
    n=read(),m=read();
    d.resize(n+1);
    ke.resize(n+1);
    foru(i,1,n) d[i]=read();
    foru(i,1,n-1)
    {
        int u,v;
        u=read(),v=read();
        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;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d",ans);
}
int main()
{
    //freopen("main.inp","r",stdin);
    //freopen("main.out","w",stdout);
    //fast;
    solve();

}

Compilation message

dyn.cpp: In function 'void dfs(int, int, int)':
dyn.cpp:33:36: warning: statement has no effect [-Wunused-value]
   33 |         if(kt[v]==-1||dem>m) {kt[u]==-1;return;}
      |                               ~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 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 204 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1080 KB Output is correct
2 Correct 25 ms 2124 KB Output is correct
3 Correct 18 ms 2700 KB Output is correct
4 Correct 30 ms 4684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 4480 KB Output is correct
2 Correct 93 ms 6124 KB Output is correct
3 Correct 108 ms 6740 KB Output is correct
4 Correct 115 ms 10052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 8576 KB Output is correct
2 Correct 131 ms 8552 KB Output is correct
3 Correct 193 ms 8336 KB Output is correct
4 Correct 267 ms 14784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 558 ms 16236 KB Output is correct
2 Correct 689 ms 18736 KB Output is correct
3 Correct 1020 ms 27956 KB Output is correct
4 Correct 1006 ms 27948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1240 ms 25716 KB Output is correct
2 Correct 814 ms 23252 KB Output is correct
3 Correct 1219 ms 28812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1135 ms 33288 KB Output is correct
2 Correct 786 ms 23360 KB Output is correct
3 Correct 1322 ms 35524 KB Output is correct
4 Correct 305 ms 23468 KB Output is correct