Submission #591523

# Submission time Handle Problem Language Result Execution time Memory
591523 2022-07-07T14:35:32 Z MohammadAghil Cat in a tree (BOI17_catinatree) C++17
100 / 100
447 ms 88588 KB
#include <iostream>
#include <algorithm>
#include <functional>
#include <random>
#include <cmath>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <cassert>
#include <string>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <limits.h>
#include <tuple>
using namespace std;

#define rep(i,l,r) for(int i = l; i < (r); i++)
#define per(i,r,l) for(int i = r; i >= (l); i--)
#define sz(x) (int)size(x)
#define pb push_back
#define all(x) begin(x), end(x)
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pp;

const ll mod = 1e9+7, maxn = 2e5 + 5, inf = ll(1e9) + 5, lg = 20;

vector<int> adj[maxn];
int cnt[maxn], h[maxn], par[maxn], min_dist[maxn], t, lgg[maxn<<1], st[maxn];
pp rmq[maxn<<1][lg];

void build(){
    rep(i,2,t+1) lgg[i] = lgg[i>>1] + 1;
    rep(j,1,lg) rep(i,0,t-(1<<j)+1) rmq[i][j] = min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
}

void dfslca(int r, int p = 0){
    st[r] = t, rmq[t++][0] = {h[r], r};
    for(int c: adj[r]) if(c - p) h[c] = h[r] + 1, dfslca(c, r), rmq[t++][0] = {h[r], r};
}

int lca(int u, int v){
    if(st[u] > st[v]) swap(u, v);
    int k = lgg[st[v] - st[u] + 1];
    return min(rmq[st[u]][k], rmq[st[v] - (1<<k) + 1][k]).ss;
}

int dist(int u, int v){
    return h[u] + h[v] - (h[lca(u, v)]<<1);
}

void dfs(int r, int p = -1){
    cnt[r] = 1;
    for(int c: adj[r]) if(par[c] == -2 && c - p) dfs(c, r), cnt[r] += cnt[c];
}

int find_cnt(int r, int bs, int p = -1){
    for(int c: adj[r]) if(c - p && par[c] == -2 && (cnt[c]<<1) > cnt[bs]) return find_cnt(c, bs, r);
    return r;
}

void dec(int r, int p = -1){
    dfs(r);
    int cn = find_cnt(r, r);
    par[cn] = p;
    for(int c: adj[cn]) if(par[c] == -2) dec(c, cn);
}

int get(int u){
    int ans = inf;
    for(int cr = u; cr + 1; cr = par[cr]) ans = min(ans, min_dist[cr] + dist(cr, u));
    return ans;
}

void upd(int u){
    for(int cr = u; cr + 1; cr = par[cr]) min_dist[cr] = min(min_dist[cr], dist(cr, u));
}

int main(){
    cin.tie(0) -> sync_with_stdio(0);
    int n, d; cin >> n >> d;
    rep(i,1,n){
        int p; cin >> p;
        adj[p].pb(i), adj[i].pb(p);
    }
    fill(par, par + n, -2);
    fill(min_dist, min_dist + n, inf);
    dec(0), dfslca(0), build();
    vector<int> node(n); iota(all(node), 0), sort(all(node), [&](int u, int v){ return h[u] > h[v]; });
    int ans = 0;
    for(int u: node){
        if(get(u) >= d) upd(u), ans++;
    }
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5024 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 4 ms 5032 KB Output is correct
8 Correct 4 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 4 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5028 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 5024 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5024 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 4 ms 5032 KB Output is correct
8 Correct 4 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 4 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5028 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 5024 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 4 ms 5716 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 4 ms 5204 KB Output is correct
25 Correct 4 ms 5332 KB Output is correct
26 Correct 3 ms 5332 KB Output is correct
27 Correct 4 ms 5332 KB Output is correct
28 Correct 5 ms 5564 KB Output is correct
29 Correct 4 ms 5588 KB Output is correct
30 Correct 4 ms 5588 KB Output is correct
31 Correct 3 ms 5588 KB Output is correct
32 Correct 3 ms 5588 KB Output is correct
33 Correct 3 ms 5588 KB Output is correct
34 Correct 4 ms 5588 KB Output is correct
35 Correct 4 ms 5588 KB Output is correct
36 Correct 3 ms 5588 KB Output is correct
37 Correct 4 ms 5616 KB Output is correct
38 Correct 4 ms 5588 KB Output is correct
39 Correct 4 ms 5576 KB Output is correct
40 Correct 4 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5024 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 4 ms 5032 KB Output is correct
8 Correct 4 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 4 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5028 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 5024 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 4 ms 5716 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 4 ms 5204 KB Output is correct
25 Correct 4 ms 5332 KB Output is correct
26 Correct 3 ms 5332 KB Output is correct
27 Correct 4 ms 5332 KB Output is correct
28 Correct 5 ms 5564 KB Output is correct
29 Correct 4 ms 5588 KB Output is correct
30 Correct 4 ms 5588 KB Output is correct
31 Correct 3 ms 5588 KB Output is correct
32 Correct 3 ms 5588 KB Output is correct
33 Correct 3 ms 5588 KB Output is correct
34 Correct 4 ms 5588 KB Output is correct
35 Correct 4 ms 5588 KB Output is correct
36 Correct 3 ms 5588 KB Output is correct
37 Correct 4 ms 5616 KB Output is correct
38 Correct 4 ms 5588 KB Output is correct
39 Correct 4 ms 5576 KB Output is correct
40 Correct 4 ms 5716 KB Output is correct
41 Correct 194 ms 77052 KB Output is correct
42 Correct 171 ms 43228 KB Output is correct
43 Correct 183 ms 43172 KB Output is correct
44 Correct 144 ms 43228 KB Output is correct
45 Correct 158 ms 43232 KB Output is correct
46 Correct 447 ms 80868 KB Output is correct
47 Correct 394 ms 80916 KB Output is correct
48 Correct 381 ms 80788 KB Output is correct
49 Correct 397 ms 80924 KB Output is correct
50 Correct 107 ms 43532 KB Output is correct
51 Correct 114 ms 43544 KB Output is correct
52 Correct 112 ms 43528 KB Output is correct
53 Correct 250 ms 81704 KB Output is correct
54 Correct 241 ms 81708 KB Output is correct
55 Correct 299 ms 81704 KB Output is correct
56 Correct 4 ms 5972 KB Output is correct
57 Correct 20 ms 14612 KB Output is correct
58 Correct 116 ms 50152 KB Output is correct
59 Correct 321 ms 88588 KB Output is correct
60 Correct 189 ms 79000 KB Output is correct
61 Correct 245 ms 76948 KB Output is correct