Submission #456493

#TimeUsernameProblemLanguageResultExecution timeMemory
456493Hamed5001Cat in a tree (BOI17_catinatree)C++14
0 / 100
4 ms5708 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mxN = 2e5+10; vector<int> adj[mxN]; int cnt[mxN]; int N, D; int bfs(int st) { memset(cnt, 0, sizeof cnt); vector<bool> vis(N+10, 0); queue<pair<int,int>> q; q.push({0, st}); vis[st] = 1; while(q.size()) { auto node = q.front(); q.pop(); cnt[node.first]++; for (auto it : adj[node.second]) { if (!vis[it]) { vis[it] = 1; q.push({node.first+1, it}); } } } int ret = 0; for (int i = 0; i <= N; i+=D) ret += cnt[i]; return ret; } void solve() { cin >> N >> D; for (int i = 1; i < N; i++) { int v; cin >> v; adj[i].push_back(v); adj[v].push_back(i); } int ans = 0; for (int i = 0; i < N; i++) { ans = max(ans, bfs(i)); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...