제출 #712111

#제출 시각아이디문제언어결과실행 시간메모리
712111Sohsoh84Cat in a tree (BOI17_catinatree)C++17
100 / 100
129 ms40656 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; #define all(x) (x).begin(),(x).end() #define X first #define Y second #define sep ' ' #define endl '\n' #define debug(x) cerr << #x << ": " << x << endl; const ll MAXN = 1e6 + 10; int D[MAXN], dp[MAXN], n, d; vector<int> adj[MAXN]; // check n == 1 void dfs(int v, int p) { if (adj[v].size() == 1 && p) { D[v] = 0; dp[v] = 1; return; } D[v] = MAXN; int mn = MAXN, mx = -1; for (int u : adj[v]) { if (u == p) continue; dfs(u, v); int td = D[u] + 1; if (2 * td >= d) { mn = min(mn, td); dp[v] += dp[u]; D[v] = min(D[v], td); } else { dp[v] += dp[u] - 1; mx = max(mx, td); } } if (mx > -1 && mx + mn >= d) { dp[v]++; D[v] = min(D[v], mx); } if (D[v] >= d) dp[v]++, D[v] = 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> d; if (n == 1) return cout << 1 << endl, 0; for (int i = 1; i < n; i++) { int v; cin >> v; adj[i + 1].push_back(v + 1); adj[v + 1].push_back(i + 1); } dfs(1, 0); cout << dp[1] << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...