Submission #1028327

#TimeUsernameProblemLanguageResultExecution timeMemory
1028327hasan2006Cat in a tree (BOI17_catinatree)C++17
0 / 100
2 ms4956 KiB
#include <bits/stdc++.h>

using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 2e5 + 9 , mod = 1e9 + 7;
ll  dp[N] , a[N] ,c[N] , d[N] ,  b[N] , p[N];
vector<int>v[N];

void dfs(int n ,int k){
    if(n)
        d[n] = d[p[n]] + 1;
    for(auto to : v[n])
        dfs(to , k);
    vector<pair<ll,ll>>vc , vv;
    vc.pb({d[n] , 1});
    for(auto to : v[n])
        vc.pb({c[to] , dp[to]});
    sort(all(vc));
    ll r =  vc.size() - 1 , sum = 0;
    for(auto to : vc)
        sum += to.se;
    for(int i = 0; i < vc.size(); i++){
        while(r >= 0 && (vc[i].fi - d[n] + vc[r].fi - d[n]) >= k)
            r--;
        ll x = sum - (r + 1) + (i <= r);
        vv.pb({x , vc[i].fi});
    }
    sort(all(vv));
    dp[n] = vv.back().fi;
    c[n] = vv.back().se;
}


void solve()
{
    ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
    cin>>n>>k;
    for( i= 1; i < n; i++){
        cin>>p[i];
        v[p[i]].pb(i);
    }
    dfs(0 , k);
    cout<<dp[0]<<"\n";
}


int main(){
    TL;
    /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int t = 1;
//    cin>>t;
    while(t--)
     {
        solve();
     }
}
// Author : حسن

Compilation message (stderr)

catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i = 0; i < vc.size(); i++){
      |                    ~~^~~~~~~~~~~
catinatree.cpp: In function 'void solve()':
catinatree.cpp:52:12: warning: unused variable 'q' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |            ^
catinatree.cpp:52:20: warning: unused variable 'j' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                    ^
catinatree.cpp:52:23: warning: unused variable 'l' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                       ^
catinatree.cpp:52:26: warning: unused variable 'r' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                          ^
catinatree.cpp:52:30: warning: unused variable 'x' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                              ^
catinatree.cpp:52:34: warning: unused variable 'y' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                  ^
catinatree.cpp:52:38: warning: unused variable 's' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                      ^
catinatree.cpp:52:46: warning: unused variable 'f' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                              ^
catinatree.cpp:52:54: warning: unused variable 'm' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                      ^
catinatree.cpp:52:58: warning: unused variable 'mn' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                          ^~
catinatree.cpp:52:69: warning: unused variable 'mx' [-Wunused-variable]
   52 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                                     ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...