Submission #975139

#TimeUsernameProblemLanguageResultExecution timeMemory
975139Halym2007Cat in a tree (BOI17_catinatree)C++17
0 / 100
1 ms2908 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second #define sz size() const int N = 1e5 + 5; int mark[N], n, d, x, vis[N]; vector <int> v[N]; void solve (int x, int pr, int dis) { if (dis >= d) { mark[x] = 1; dis = 0; } for (int i : v[x]) { if (i == pr) continue; solve (i, x, dis + 1); } } int dis[N]; int main () { // freopen ("input.txt", "r", stdin); cin >> n >> d; for (int i = 1; i < n; ++i) { int x; cin >> x; v[x].pb (i); v[i].pb (x); } queue <int> q; q.push (0); vis[0] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int i : v[x]) { if (vis[i]) continue; q.push (i); dis[i] = dis[x] + 1; vis[i] = 1; } } int node = 0; for (int i = 0; i < n; ++i) { if (dis[node] < dis[i]) { node = i; } } solve (node, -1, d); int ret = 0; for (int i = 0; i < n; ++i) { ret += mark[i] == 1; } cout << ret; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...