Submission #569056

# Submission time Handle Problem Language Result Execution time Memory
569056 2022-05-26T14:31:49 Z minhcool Cat in a tree (BOI17_catinatree) C++17
0 / 100
4 ms 4968 KB
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int N = 2e5 + 5;

const int oo = 1e18 + 7, mod = 1e9 + 7;

int n, d;

vector<int> Adj[N];

int dp[N];

ii small[N];

void dfs(int u){
	//cout << "OK " << u << "\n";
	for(auto v : Adj[u]){
		dfs(v);
		small[v].fi++, small[v].se++;
	}
	set<ii> se;
	for(auto v : Adj[u]){
		if(small[v].fi * 2 >= d){
			se.insert({small[v].fi, v});
		}
		else{
			se.insert({small[v].se, v});
		}
	}
	if(!se.size()){
		dp[u] = 1;
		small[u] = {0, oo};
		return;
	}
	int mx = -oo;
	for(auto v : Adj[u]){
		if(small[v].fi * 2 >= d){
			dp[u] += dp[v];
			continue;
		}
		//dp[u] += (dp[v] - 1);
		if(small[v].fi * 2 >= d){
			se.erase({small[v].fi, v});
		}
		else{
			se.erase({small[v].se, v});
		}
		if(!se.size() || ((*se.begin()).fi + small[v].fi) >= d) mx = max(mx, small[v].fi);
		if(small[v].fi * 2 >= d){
			se.insert({small[v].fi, v});
		}
		else{
			se.insert({small[v].se, v});
		}
	}
	if(mx != -oo){
		dp[u]++;
		small[u] = {mx, (*se.begin()).fi};
	}
	else{
		small[u].fi = (*se.begin()).fi;
		se.erase(se.begin());
		if(se.size()) small[u].se = (*se.begin()).fi;
		else small[u].se = oo;
	}
	if(small[u].fi >= d){
		dp[u]++;
		small[u].fi = 0, small[u].se = d;
	}
}

void process(){
	cin >> n >> d;
	//cout << n << " " << d << "\n";
	for(int i = 2; i <= n; i++){
		int x;
		cin >> x;
		Adj[x + 1].pb(i);
		//cout << x << " " << i << "\n";
	}
	dfs(1);
	cout << dp[1];
}

signed main(){
	ios_base::sync_with_stdio(0);
	process();
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Incorrect 3 ms 4968 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Incorrect 3 ms 4968 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Incorrect 3 ms 4968 KB Output isn't correct
3 Halted 0 ms 0 KB -