Submission #591510

# Submission time Handle Problem Language Result Execution time Memory
591510 2022-07-07T14:21:00 Z MohammadAghil Cat in a tree (BOI17_catinatree) C++17
100 / 100
640 ms 39884 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], nxt[maxn][lg];

void dfslca(int r, int p = 0){
    nxt[r][0] = p;
    rep(i,1,lg) nxt[r][i] = nxt[nxt[r][i-1]][i-1];
    for(int c: adj[r]) if(c - p) h[c] = h[r] + 1, dfslca(c, r);
}

int lca(int u, int v){
    if(h[u] > h[v]) swap(u, v);
    per(i,lg-1,0) if(h[nxt[v][i]] >= h[u]) v = nxt[v][i];
    if(u == v) return u;
    per(i,lg-1,0) if(nxt[u][i] - nxt[v][i]) u = nxt[u][i], v = nxt[v][i];
    return nxt[u][0];
}

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);
    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 3 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 3 ms 4968 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5028 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4952 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5028 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5028 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 3 ms 4968 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5028 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4952 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5028 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5028 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 5 ms 5332 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 7 ms 5008 KB Output is correct
24 Correct 3 ms 5036 KB Output is correct
25 Correct 5 ms 5076 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 5 ms 5204 KB Output is correct
30 Correct 5 ms 5168 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 5 ms 5204 KB Output is correct
34 Correct 4 ms 5196 KB Output is correct
35 Correct 4 ms 5160 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 4 ms 5264 KB Output is correct
38 Correct 5 ms 5164 KB Output is correct
39 Correct 4 ms 5204 KB Output is correct
40 Correct 5 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 3 ms 4968 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5028 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4952 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5028 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5028 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 5 ms 5332 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 7 ms 5008 KB Output is correct
24 Correct 3 ms 5036 KB Output is correct
25 Correct 5 ms 5076 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 5 ms 5204 KB Output is correct
30 Correct 5 ms 5168 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 5 ms 5204 KB Output is correct
34 Correct 4 ms 5196 KB Output is correct
35 Correct 4 ms 5160 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 4 ms 5264 KB Output is correct
38 Correct 5 ms 5164 KB Output is correct
39 Correct 4 ms 5204 KB Output is correct
40 Correct 5 ms 5368 KB Output is correct
41 Correct 157 ms 30628 KB Output is correct
42 Correct 302 ms 18600 KB Output is correct
43 Correct 201 ms 18500 KB Output is correct
44 Correct 198 ms 18604 KB Output is correct
45 Correct 229 ms 18576 KB Output is correct
46 Correct 601 ms 32192 KB Output is correct
47 Correct 506 ms 32216 KB Output is correct
48 Correct 499 ms 32200 KB Output is correct
49 Correct 532 ms 32172 KB Output is correct
50 Correct 137 ms 18836 KB Output is correct
51 Correct 158 ms 18832 KB Output is correct
52 Correct 139 ms 18764 KB Output is correct
53 Correct 320 ms 32600 KB Output is correct
54 Correct 331 ms 32588 KB Output is correct
55 Correct 315 ms 32656 KB Output is correct
56 Correct 7 ms 5460 KB Output is correct
57 Correct 42 ms 9420 KB Output is correct
58 Correct 237 ms 25540 KB Output is correct
59 Correct 640 ms 39884 KB Output is correct
60 Correct 173 ms 31432 KB Output is correct
61 Correct 313 ms 30624 KB Output is correct