제출 #650446

#제출 시각아이디문제언어결과실행 시간메모리
650446LoboCat in a tree (BOI17_catinatree)C++17
100 / 100
80 ms22692 KiB
#include<bits/stdc++.h> using namespace std; const long long inf = (long long) 1e18 + 10; const int inf1 = (int) 1e9 + 10; #define int long long #define dbl long double #define endl '\n' #define sc second #define fr first #define mp make_pair #define pb push_back #define all(x) x.begin(), x.end() const int maxn = 2e5+10; int n,D, dpq[maxn], dp1[maxn], dp2[maxn]; vector<int> g[maxn]; // quantity of marked nodes // distance from the closest marked node // distance from the closest marked node more than (D+1)/2 void dfs(int u) { dpq[u] = 0; dp1[u] = 0; dp2[u] = D+1; if(g[u].size() == 0) return; for(auto v : g[u]) { dfs(v); dpq[u]+= dpq[v]; dp2[u] = min(dp2[u],dp2[v]+1); if(dp1[v]+1 >= (D+1)/2) { dpq[u]++; dp2[u] = min(dp2[u],dp1[v]+1); } else { dp1[u] = max(dp1[u],dp1[v]+1); } } if(dp1[u]+dp2[u] < D) { dp1[u] = -inf; } if(u == 1 && dp1[u]+dp2[u] >= D) { dpq[u]++; } // cout << u << " " << dp1[u] << " " << dp2[u] << endl; } void solve() { cin >> n >> D; for(int i = 2; i <= n; i++) { int x; cin >> x; g[x+1].pb(i); } dfs(1); cout << dpq[1] << endl; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("in.in", "r", stdin); // freopen("out.out", "w", stdout); int tt = 1; // cin >> tt; while(tt--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...