답안 #650446

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
650446 2022-10-13T18:38:57 Z Lobo Cat in a tree (BOI17_catinatree) C++17
100 / 100
80 ms 22692 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 2e5+10;

int n,D, dpq[maxn], dp1[maxn], dp2[maxn];
vector<int> g[maxn];

// quantity of marked nodes
// distance from the closest marked node
// distance from the closest marked node more than (D+1)/2

void dfs(int u) {
    dpq[u] = 0;
    dp1[u] = 0;
    dp2[u] = D+1;
    if(g[u].size() == 0) return;

    for(auto v : g[u]) {
        dfs(v);
        dpq[u]+= dpq[v];

        dp2[u] = min(dp2[u],dp2[v]+1);
        if(dp1[v]+1 >= (D+1)/2) {
            dpq[u]++;
            dp2[u] = min(dp2[u],dp1[v]+1);
        }
        else {
            dp1[u] = max(dp1[u],dp1[v]+1);
        }
    }

    if(dp1[u]+dp2[u] < D) {
        dp1[u] = -inf;
    }

    if(u == 1 && dp1[u]+dp2[u] >= D) {
        dpq[u]++;
    }
   // cout << u << " " << dp1[u] << " " << dp2[u] << endl;
}

void solve() {
    cin >> n >> D;
    for(int i = 2; i <= n; i++) {
        int x; cin >> x;
        g[x+1].pb(i);
    }

    dfs(1);
    cout << dpq[1] << endl;

}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5024 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 4948 KB Output is correct
9 Correct 3 ms 4988 KB Output is correct
10 Correct 3 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 3 ms 5028 KB Output is correct
14 Correct 3 ms 5028 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5028 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 5028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5024 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 4948 KB Output is correct
9 Correct 3 ms 4988 KB Output is correct
10 Correct 3 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 3 ms 5028 KB Output is correct
14 Correct 3 ms 5028 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5028 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 5028 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5028 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 3 ms 5036 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 3 ms 5028 KB Output is correct
33 Correct 4 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5024 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 4948 KB Output is correct
9 Correct 3 ms 4988 KB Output is correct
10 Correct 3 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 3 ms 5028 KB Output is correct
14 Correct 3 ms 5028 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5028 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 5028 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5028 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 3 ms 5036 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 3 ms 5028 KB Output is correct
33 Correct 4 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5204 KB Output is correct
41 Correct 36 ms 14364 KB Output is correct
42 Correct 29 ms 9788 KB Output is correct
43 Correct 27 ms 9772 KB Output is correct
44 Correct 32 ms 9784 KB Output is correct
45 Correct 29 ms 9780 KB Output is correct
46 Correct 67 ms 14372 KB Output is correct
47 Correct 73 ms 14540 KB Output is correct
48 Correct 66 ms 14412 KB Output is correct
49 Correct 80 ms 14428 KB Output is correct
50 Correct 15 ms 8832 KB Output is correct
51 Correct 14 ms 8788 KB Output is correct
52 Correct 15 ms 8828 KB Output is correct
53 Correct 29 ms 12712 KB Output is correct
54 Correct 26 ms 12712 KB Output is correct
55 Correct 30 ms 12704 KB Output is correct
56 Correct 4 ms 5204 KB Output is correct
57 Correct 7 ms 7764 KB Output is correct
58 Correct 24 ms 17712 KB Output is correct
59 Correct 63 ms 22692 KB Output is correct
60 Correct 41 ms 15044 KB Output is correct
61 Correct 51 ms 14604 KB Output is correct