Submission #712110

# Submission time Handle Problem Language Result Execution time Memory
712110 2023-03-18T06:51:36 Z Sohsoh84 Cat in a tree (BOI17_catinatree) C++17
0 / 100
11 ms 23764 KB
#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], D[u]);
		} else {
			dp[v] += dp[u] - 1;
			mx = max(mx, td);
		}
	}

	if (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 time Memory Grader output
1 Incorrect 11 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -