Submission #134953

#TimeUsernameProblemLanguageResultExecution timeMemory
134953songcCat in a tree (BOI17_catinatree)C++14
100 / 100
208 ms16484 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

int N, K, ans;
int D[202020], dep[202020];
vector<int> adj[202020];
vector<pii> V;

void dfs(int u, int p, int d){
	if (D[u] >= d) return;
	D[u] = d;
	for (int v : adj[u]) if (v != p) dfs(v, u, d-1);
}

int main(){
	scanf("%d %d", &N, &K);
	for (int i=2; i<=N; i++){
		int x;
		scanf("%d", &x);
		adj[x+1].push_back(i);
		adj[i].push_back(x+1);
		dep[i] = dep[x+1] + 1;
	}
	for (int i=1; i<=N; i++) V.push_back(pii(-dep[i], i));
	sort(V.begin(), V.end());
	for (int i=0; i<N; i++){
		int u = V[i].second;
		if (D[u]) continue;
		ans++;
		dfs(u, 0, K);
	} 
	printf("%d\n", ans);
	return 0;
}

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~~
catinatree.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &x);
   ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...