Submission #849269

#TimeUsernameProblemLanguageResultExecution timeMemory
849269Vu_CG_CoderCat in a tree (BOI17_catinatree)C++14
51 / 100
35 ms15312 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; int n , d , res = 0; vector <int> a[N]; int high[N], oder[N], value[N], par[N]; void dfs(int u){ for (int v : a[u]) if (v != par[u]){ high[v] = high[u] + 1; par[v] = u; dfs(v); } } int get_min(int u){ int val = 1e9 , x = 0; while (u != 0){ val = min(val,value[u] + x); u = par[u], x++; } return val; } void update(int u){ int val = 0; res++; while (u != 0){ value[u] = min(value[u],val); u = par[u]; val++; } } bool cmp(int x ,int y){return high[x] > high[y];} int main(){ //freopen("txt.inp","r",stdin); //freopen("txt.out","w",stdout); cin >> n >> d; for (int i = 2; i <= n ; i++){ int x; cin >> x; x++; a[x].push_back(i); a[i].push_back(x); } for (int i = 1 ; i < n ; i++){ // int x , y; // cin >> x >> y; // a[x].push_back(y); // a[y].push_back(x); oder[i] = i; } oder[n] = n; par[1] = 0; dfs(1); sort(oder + 1,oder + n + 1,cmp); memset(value,0x3f,sizeof(value)); for (int i = 1 ; i <= n ; i++) if (get_min(oder[i]) >= d) update(oder[i]); cout << res; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...