Submission #1038991

#TimeUsernameProblemLanguageResultExecution timeMemory
1038991HanksburgerCat in a tree (BOI17_catinatree)C++17
0 / 100
1 ms5208 KiB
#include <bits/stdc++.h> using namespace std; int deg[200005], visited[200005], n, d, ans; vector<int> adj[200005]; queue<int> q; void dfs(int u, int x, int y) { visited[u]=y; for (int v:adj[u]) { if (visited[v]!=y && x<d) dfs(v, x+1, y); else if (!visited[v]) { deg[v]--; if (deg[v]==1) q.push(v); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> d; for (int i=1; i<n; i++) { int x; cin >> x; adj[i].push_back(x); adj[x].push_back(i); deg[i]++, deg[x]++; } for (int i=0; i<n; i++) if (deg[i]==1) q.push(i); while (!q.empty()) { int u=q.front(); q.pop(); if (visited[u]) continue; ans++; dfs(u, 1, u+1); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...