Submission #647780

# Submission time Handle Problem Language Result Execution time Memory
647780 2022-10-04T06:06:50 Z mychecksedad Cat in a tree (BOI17_catinatree) C++17
0 / 100
17 ms 23936 KB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;


int n, d, best = 0, ans, dep[N], mx[N], mxc[N];
vector<int> g[N];
void pre(int v, int p){
    dep[v] = dep[p] + 1;
    mx[v] = dep[v];
    mxc[v] = -1;
    for(int u: g[v]){
        if(u != p){
            pre(u, v);
            if(mx[v] < mx[u]){
                mxc[v] = u;
            }
            mx[v] = max(mx[v], mx[u]);
        }
    }
}
void dfs(int v, int p, int min_dist){
    if(min_dist == d){
        ans++;
        min_dist = 0;
    }
    bool ok = 1;
    for(int u: g[v]){
        if(u != p){
            if(ok && mxc[v] == u) dfs(u, v, min_dist + 1);
            else dfs(u, v, min(min_dist + 1, d - min_dist + 1));
            ok = 0;
        }
    }
}

void solve(){
    cin >> n >> d;
    for(int i = 0; i < n-1; ++i){
        int u, v; cin >> u;
        v = i + 1;
        g[v].pb(u);
        g[u].pb(v);
    }
    for(int i = 0; i < n; ++i){
        ans = 0;
        dep[i] = 0;
        pre(i, i);
        dfs(i, i, d);
        best = max(best, ans);
    }
    cout << best;
}





int main(){
    cin.tie(0); ios::sync_with_stdio(0);
    int T = 1, aa;
    // cin >> T;aa=T;
    while(T--){
        // cout << "Case #" << aa-T << ": ";
        solve();
        cout << '\n';
    }
    return 0;
 
}

Compilation message

catinatree.cpp: In function 'int main()':
catinatree.cpp:74:16: warning: unused variable 'aa' [-Wunused-variable]
   74 |     int T = 1, aa;
      |                ^~
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23936 KB Output isn't correct
2 Halted 0 ms 0 KB -