제출 #1038996

#제출 시각아이디문제언어결과실행 시간메모리
1038996HanksburgerCat in a tree (BOI17_catinatree)C++17
0 / 100
1 ms4956 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; if (n==1) { cout << 1; return 0; } 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...