Submission #347200

#TimeUsernameProblemLanguageResultExecution timeMemory
347200parsabahramiCat in a tree (BOI17_catinatree)C++17
100 / 100
110 ms14572 KiB
// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 2e5 + 10, MOD = 1e9 + 7;
int mx[N], dp[N], n, D;
vector<int> adj[N];

void DFS(int v) {
    if (!SZ(adj[v])) {
        dp[v] = 1, mx[v] = 0;
        return;
    }
    for (int u : adj[v]) DFS(u);
    sort(adj[v].begin(), adj[v].end(), [&](int u, int w) {
            return mx[u] > mx[w];
    });
    int M = MOD, res = 0;
    for (int u : adj[v]) {
        if (mx[u] + M + 2 >= D) {
            res += dp[u], M = mx[u];
        } else res += dp[u] - 1;
    }
    if (M >= D - 1) {
        M = -1, res++;
    }
    dp[v] = res, mx[v] = M + 1;
}

int main() {
    scanf("%d%d", &n, &D);
    for (int i = 1; i < n; i++) {
        int p; scanf("%d", &p);
        adj[p].push_back(i);
    }
    DFS(0);
    printf("%d\n", dp[0]);
    return 0;
}

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |     scanf("%d%d", &n, &D);
      |     ~~~~~^~~~~~~~~~~~~~~~
catinatree.cpp:41:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |         int p; scanf("%d", &p);
      |                ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...