Submission #22827823


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
#define ll long long
const int areTests = 0, N = 2e5 + 20;

int n, p[N], q, u, d, dp[N], st[N], ed[N], tym, a[N];
vector <int> adj[N], idx[N];

void dfs(int node, int edges) {
	st[node] = ++tym;
	dp[node] = edges;
	idx[edges].push_back(tym);
	for(int child: adj[node]) {
		dfs(child, edges + 1);
	}
	ed[node] = tym;
}

void run_test(int testcase) {
	cin >> n;
	for(int i = 2; i <= n; i++) {
		cin >> p[i];
		adj[p[i]].push_back(i);
	}

	dfs(1, 0);
	for(int i = 0; i <= n; i++) {
		sort(idx[i].begin(), idx[i].end());
	}

	cin >> q;
	while(q--) {
		cin >> u >> d;

		if(dp[u] > d) {
			cout << "0";
		} else {
			cout << upper_bound(idx[d].begin(), idx[d].end(), ed[u]) - lower_bound(idx[d].begin(), idx[d].end(), st[u]);
		}
		cout << "\n";
	}
}

int main() {
	ios::sync_with_stdio(0);
	#ifndef DUSH1729
	cin.tie(0);
	#endif
	cout << fixed << setprecision(10);

	int t = 1;
	if(areTests) cin >> t;
	for(int i = 1; i <= t; i++) {
		run_test(i);
	}
}

Submission Info

Submission Time
Task E - Count Descendants
User dush1729
Language C++ (GCC 9.2.1)
Score 500
Code Size 1049 Byte
Status AC
Exec Time 131 ms
Memory 38008 KiB

Compile Error

./Main.cpp: In function ‘void run_test(int)’:
./Main.cpp:19:19: warning: unused parameter ‘testcase’ [-Wunused-parameter]
   19 | void run_test(int testcase) {
      |               ~~~~^~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 30
Set Name Test Cases
Sample sample_00
All binary_00, binary_01, binary_02, binary_03, binary_04, binary_05, binary_06, binary_07, binary_08, binary_09, bound_00, bound_01, bound_02, bound_03, broomlike_00, broomlike_01, broomlike_02, broomlike_03, broomlike_04, random_00, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, sample_00
Case Name Status Exec Time Memory
binary_00 AC 87 ms 17864 KiB
binary_01 AC 82 ms 16012 KiB
binary_02 AC 73 ms 17560 KiB
binary_03 AC 56 ms 17948 KiB
binary_04 AC 83 ms 17548 KiB
binary_05 AC 57 ms 16656 KiB
binary_06 AC 47 ms 17568 KiB
binary_07 AC 34 ms 15080 KiB
binary_08 AC 74 ms 17108 KiB
binary_09 AC 114 ms 19428 KiB
bound_00 AC 131 ms 38008 KiB
bound_01 AC 128 ms 37940 KiB
bound_02 AC 48 ms 12952 KiB
bound_03 AC 49 ms 12912 KiB
broomlike_00 AC 90 ms 25160 KiB
broomlike_01 AC 85 ms 26804 KiB
broomlike_02 AC 103 ms 25776 KiB
broomlike_03 AC 93 ms 22044 KiB
broomlike_04 AC 87 ms 26200 KiB
random_00 AC 42 ms 15396 KiB
random_01 AC 94 ms 19164 KiB
random_02 AC 64 ms 14056 KiB
random_03 AC 84 ms 15000 KiB
random_04 AC 113 ms 19764 KiB
random_05 AC 41 ms 13376 KiB
random_06 AC 97 ms 17584 KiB
random_07 AC 99 ms 16788 KiB
random_08 AC 78 ms 19004 KiB
random_09 AC 109 ms 19676 KiB
sample_00 AC 15 ms 12952 KiB