제출 #1038979

#제출 시각아이디문제언어결과실행 시간메모리
1038979HanksburgerCat in a tree (BOI17_catinatree)C++17
0 / 100
2 ms5160 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) { visited[u]=1; for (int v:adj[u]) { if (visited[v]) continue; if (x<d) dfs(v, x+1); else { 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); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...