제출 #796366

#제출 시각아이디문제언어결과실행 시간메모리
796366SlavicGCat in a tree (BOI17_catinatree)C++17
0 / 100
2 ms4948 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
const int N = 2e5 + 5;
vector<int> adj[N];
int cnt[N], n, d;

void dfs(int u, int par, int dist) {
	++cnt[dist % d];
	for(int v: adj[u]) {
		if(v == par) continue;
		dfs(v, u, dist + 1);
	}
}

void solve() {
	cin >> n >> d;
	for(int i = 1; i < n; ++i) {
		int x; cin >> x;
		adj[x].pb(i);
		adj[i].pb(x);
	}
	int ans = 0;
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) cnt[j] = 0;
		dfs(i, -1, 0);
		ans = max(ans, cnt[0]);
	}
	cout << ans << '\n';
}

int32_t main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		solve();
	}
}
/*
4 3
0
0
1

3 1000
0
0
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...