Submission #849268

# Submission time Handle Problem Language Result Execution time Memory
849268 2023-09-14T10:51:21 Z Vu_CG_Coder Cat in a tree (BOI17_catinatree) C++14
0 / 100
1000 ms 3928 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int n , d , res = 0;
vector <int> a[N];
int high[N], oder[N], value[N], par[N];

void dfs(int u){
    for (int v : a[u])
    if (v != par[u]){
        high[v] = high[u] + 1;
        par[v] = u;
        dfs(v);
    }
}

int get_min(int u){
    int val = 1e9 , x = 0;
    while (u != 0){
        val = min(val,value[u] + x);
        u = par[u], x++;
    }
    return val;
}

int update(int u){
    int val = 0; res++;
    while (u != 0){
        value[u] = min(value[u],val);
        u = par[u]; val++;
    }
}

bool cmp(int x ,int y){return high[x] > high[y];}
int main(){
    //freopen("txt.inp","r",stdin);
   // freopen("txt.out","w",stdout);
    cin >> n >> d;
    for (int i = 2; i <= n ; i++){
        int x; cin >> x; x++;
        a[x].push_back(i);
        a[i].push_back(x);
    }

    for (int i = 1 ; i < n ; i++){
        // int x , y;
        // cin >> x >> y;
        // a[x].push_back(y);
        // a[y].push_back(x);
        oder[i] = i;
    }
    oder[n] = n; par[1] = 0;
    dfs(1);
    sort(oder + 1,oder + n + 1,cmp);
    memset(value,0x3f,sizeof(value));
    for (int i = 1 ; i <= n ; i++)
        if (get_min(oder[i]) >= d) update(oder[i]);

    cout << res;
    return 0;
}

Compilation message

catinatree.cpp: In function 'int update(int)':
catinatree.cpp:32:1: warning: no return statement in function returning non-void [-Wreturn-type]
   32 | }
      | ^
# Verdict Execution time Memory Grader output
1 Execution timed out 1042 ms 3928 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1042 ms 3928 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1042 ms 3928 KB Time limit exceeded
2 Halted 0 ms 0 KB -