Submission #480880

# Submission time Handle Problem Language Result Execution time Memory
480880 2021-10-18T14:32:54 Z hidden1 Cat in a tree (BOI17_catinatree) C++14
100 / 100
97 ms 40672 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for(int i = 0; i < n; i ++)
#define revn(i, n) for(int i = n - 1; i >= 0; i --)
typedef long long ll;
template<class T, template<class T2, class A=allocator<T2> > class cont> inline ostream &operator <<(ostream &out, const cont<T> &x) { for(const auto &it : x) { out << it << " ";} return out;}
template<class T, template<class T2, class A=allocator<T2> > class cont> inline istream &operator >>(istream &in, cont<T> &x) { for(auto &it : x) { in >> it;} return in;}
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;

const int MAX_N = 1e6 + 10;
vector<int> g[MAX_N];
int n, d;

pair<int, int> dfs(int x, int p) {
    vector<pair<int, pair<int, int> > > child;  
    int ret = 0;
    int mn = mod;

    for(auto it : g[x]) {   
        if(it == p) {continue;}
        child.push_back({it, dfs(it, x)});
        ret += child.back().second.first;
    }

    mn = mod;

    sort(all(child), [](const pair<int, pair<int, int> > &a, const pair<int, pair<int, int> > &b){
        return a.second.second < b.second.second;
    });

    if(!child.empty()) {
        mn = child.back().second.second;
    }
    revn(i, child.size() - 1) {
        if(child[i].second.second + child[i + 1].second.second < d) {
            ret --;
        } else {
            chkmin(mn, child[i].second.second);
        }
    }

    if(mn >= d) {
        mn = 0;
        ret ++;
    }

    cerr << "Returning from " << x << " " << p << " -> " << ret << " " << mn + 1 << endl; 

    return {ret, mn + 1};
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    cin >> n >> d;

    forn(i, n - 1) {
        int p;
        cin >> p;
        g[p].push_back(i + 1);
    }

    auto ans = dfs(0, -1).first;

    cout << ans << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 12 ms 23792 KB Output is correct
4 Correct 13 ms 23744 KB Output is correct
5 Correct 13 ms 23712 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 13 ms 23776 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 13 ms 23796 KB Output is correct
10 Correct 13 ms 23788 KB Output is correct
11 Correct 13 ms 23756 KB Output is correct
12 Correct 12 ms 23736 KB Output is correct
13 Correct 12 ms 23756 KB Output is correct
14 Correct 12 ms 23796 KB Output is correct
15 Correct 13 ms 23684 KB Output is correct
16 Correct 13 ms 23776 KB Output is correct
17 Correct 12 ms 23756 KB Output is correct
18 Correct 13 ms 23720 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 13 ms 23776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 12 ms 23792 KB Output is correct
4 Correct 13 ms 23744 KB Output is correct
5 Correct 13 ms 23712 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 13 ms 23776 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 13 ms 23796 KB Output is correct
10 Correct 13 ms 23788 KB Output is correct
11 Correct 13 ms 23756 KB Output is correct
12 Correct 12 ms 23736 KB Output is correct
13 Correct 12 ms 23756 KB Output is correct
14 Correct 12 ms 23796 KB Output is correct
15 Correct 13 ms 23684 KB Output is correct
16 Correct 13 ms 23776 KB Output is correct
17 Correct 12 ms 23756 KB Output is correct
18 Correct 13 ms 23720 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 13 ms 23776 KB Output is correct
21 Correct 14 ms 23972 KB Output is correct
22 Correct 13 ms 23804 KB Output is correct
23 Correct 13 ms 23804 KB Output is correct
24 Correct 12 ms 23756 KB Output is correct
25 Correct 13 ms 23812 KB Output is correct
26 Correct 13 ms 23756 KB Output is correct
27 Correct 13 ms 23748 KB Output is correct
28 Correct 14 ms 23700 KB Output is correct
29 Correct 13 ms 23756 KB Output is correct
30 Correct 13 ms 23808 KB Output is correct
31 Correct 12 ms 23756 KB Output is correct
32 Correct 14 ms 23756 KB Output is correct
33 Correct 15 ms 23728 KB Output is correct
34 Correct 13 ms 23820 KB Output is correct
35 Correct 14 ms 23816 KB Output is correct
36 Correct 13 ms 23780 KB Output is correct
37 Correct 13 ms 23756 KB Output is correct
38 Correct 12 ms 23848 KB Output is correct
39 Correct 14 ms 23820 KB Output is correct
40 Correct 13 ms 24012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 12 ms 23792 KB Output is correct
4 Correct 13 ms 23744 KB Output is correct
5 Correct 13 ms 23712 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 13 ms 23776 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 13 ms 23796 KB Output is correct
10 Correct 13 ms 23788 KB Output is correct
11 Correct 13 ms 23756 KB Output is correct
12 Correct 12 ms 23736 KB Output is correct
13 Correct 12 ms 23756 KB Output is correct
14 Correct 12 ms 23796 KB Output is correct
15 Correct 13 ms 23684 KB Output is correct
16 Correct 13 ms 23776 KB Output is correct
17 Correct 12 ms 23756 KB Output is correct
18 Correct 13 ms 23720 KB Output is correct
19 Correct 13 ms 23756 KB Output is correct
20 Correct 13 ms 23776 KB Output is correct
21 Correct 14 ms 23972 KB Output is correct
22 Correct 13 ms 23804 KB Output is correct
23 Correct 13 ms 23804 KB Output is correct
24 Correct 12 ms 23756 KB Output is correct
25 Correct 13 ms 23812 KB Output is correct
26 Correct 13 ms 23756 KB Output is correct
27 Correct 13 ms 23748 KB Output is correct
28 Correct 14 ms 23700 KB Output is correct
29 Correct 13 ms 23756 KB Output is correct
30 Correct 13 ms 23808 KB Output is correct
31 Correct 12 ms 23756 KB Output is correct
32 Correct 14 ms 23756 KB Output is correct
33 Correct 15 ms 23728 KB Output is correct
34 Correct 13 ms 23820 KB Output is correct
35 Correct 14 ms 23816 KB Output is correct
36 Correct 13 ms 23780 KB Output is correct
37 Correct 13 ms 23756 KB Output is correct
38 Correct 12 ms 23848 KB Output is correct
39 Correct 14 ms 23820 KB Output is correct
40 Correct 13 ms 24012 KB Output is correct
41 Correct 53 ms 28768 KB Output is correct
42 Correct 39 ms 25920 KB Output is correct
43 Correct 40 ms 25840 KB Output is correct
44 Correct 42 ms 25980 KB Output is correct
45 Correct 39 ms 25908 KB Output is correct
46 Correct 78 ms 28100 KB Output is correct
47 Correct 97 ms 28284 KB Output is correct
48 Correct 76 ms 28156 KB Output is correct
49 Correct 81 ms 28224 KB Output is correct
50 Correct 26 ms 24780 KB Output is correct
51 Correct 27 ms 24652 KB Output is correct
52 Correct 28 ms 24748 KB Output is correct
53 Correct 37 ms 25600 KB Output is correct
54 Correct 42 ms 25604 KB Output is correct
55 Correct 41 ms 25676 KB Output is correct
56 Correct 15 ms 23884 KB Output is correct
57 Correct 18 ms 27016 KB Output is correct
58 Correct 44 ms 38428 KB Output is correct
59 Correct 79 ms 40672 KB Output is correct
60 Correct 55 ms 29920 KB Output is correct
61 Correct 65 ms 28868 KB Output is correct