Submission #456490

#TimeUsernameProblemLanguageResultExecution timeMemory
456490Hamed5001Cat in a tree (BOI17_catinatree)C++14
0 / 100
3 ms4940 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; void bfs(int st) { 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}); } } } } 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); } for (int i = 0; i < N; i++) { if (adj[i].size() == 1) { bfs(i); break; } } int ans = 0; for (int i = 0; i <= N; i+=D) ans += cnt[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...