Submission #440945

# Submission time Handle Problem Language Result Execution time Memory
440945 2021-07-03T15:00:23 Z S2speed Cat in a tree (BOI17_catinatree) C++17
100 / 100
131 ms 23472 KB
#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin() , x.end()
#define sze(x) (ll)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
typedef pair<pll , bool> pllb;

const ll maxn = 2e5 + 16 , md = 1e9 + 7;

ll tav(ll n , ll k){
	ll res = 1;
	while(k > 0){
		if(k & 1){
			res *= n; res %= md;
		}
		n *= n; n %= md;
		k >>= 1;
	}
	return res;
}
 
ll n , d , sq = 300;
ll par[maxn] , dis[maxn] , cl[maxn] , ds[maxn];
vector<pll> u;
bitset<maxn> far;
vector<ll> bfs , adj[maxn];

void BFS(ll r){
	ds[r] = 0;
	bfs.push_back(r);
	ll x = 0;
	while(x < sze(bfs)){
		ll v = bfs[x++];
		if(ds[v] == d - 1) break;
		for(auto i : adj[v]){
			if(ds[i] > ds[v] + 1){
				ds[i] = ds[v] + 1;
				bfs.push_back(i);
				far[i] = false;
			}
		}
	}
	bfs.clear();
	return;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	memset(cl , 63 , sizeof(cl));
	par[0] = -1; dis[0] = 0;
	cin>>n>>d;
	u.push_back({0 , 0});
	for(ll i = 1 ; i < n ; i++){
		cin>>par[i];
		dis[i] = dis[par[i]] + 1;
		u.push_back({dis[i] , i});
	}
	sort(all(u) , greater<pll>());
	ll ans = 0 , lm = (d - 1) >> 1;
	if(d < sq){
		for(ll e = 0 ; e < n ; e++){
			ll i = u[e].second , v = i;
			bool ch = true;
			for(ll j = 0 ; j <= lm && v != -1 ; j++){
				ch &= (cl[v] + j >= d);
				v = par[v];
			}
			if(!ch) continue;
			ans++;
			v = par[i];
			for(ll j = 1 ; j < d && v != -1 ; j++){
				cl[v] = j;
				v = par[v];
			}
		}
		cout<<ans<<'\n';
		return 0;
	}
	far.set();
	memset(ds , 63 , sizeof(ds));
	for(ll i = 1 ; i < n ; i++){
		adj[i].push_back(par[i]);
		adj[par[i]].push_back(i);
	}
	for(ll e = 0 ; e < n ; e++){
		ll i = u[e].second;
		if(far[i]){
			ans++;
			BFS(i);
		}
	}
	cout<<ans<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6596 KB Output is correct
2 Correct 5 ms 6596 KB Output is correct
3 Correct 4 ms 6476 KB Output is correct
4 Correct 4 ms 6476 KB Output is correct
5 Correct 5 ms 6604 KB Output is correct
6 Correct 7 ms 6476 KB Output is correct
7 Correct 5 ms 6596 KB Output is correct
8 Correct 5 ms 6476 KB Output is correct
9 Correct 7 ms 6604 KB Output is correct
10 Correct 4 ms 6596 KB Output is correct
11 Correct 5 ms 6476 KB Output is correct
12 Correct 4 ms 6604 KB Output is correct
13 Correct 4 ms 6604 KB Output is correct
14 Correct 5 ms 6600 KB Output is correct
15 Correct 4 ms 6476 KB Output is correct
16 Correct 5 ms 6476 KB Output is correct
17 Correct 7 ms 6600 KB Output is correct
18 Correct 5 ms 6596 KB Output is correct
19 Correct 4 ms 6476 KB Output is correct
20 Correct 4 ms 6476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6596 KB Output is correct
2 Correct 5 ms 6596 KB Output is correct
3 Correct 4 ms 6476 KB Output is correct
4 Correct 4 ms 6476 KB Output is correct
5 Correct 5 ms 6604 KB Output is correct
6 Correct 7 ms 6476 KB Output is correct
7 Correct 5 ms 6596 KB Output is correct
8 Correct 5 ms 6476 KB Output is correct
9 Correct 7 ms 6604 KB Output is correct
10 Correct 4 ms 6596 KB Output is correct
11 Correct 5 ms 6476 KB Output is correct
12 Correct 4 ms 6604 KB Output is correct
13 Correct 4 ms 6604 KB Output is correct
14 Correct 5 ms 6600 KB Output is correct
15 Correct 4 ms 6476 KB Output is correct
16 Correct 5 ms 6476 KB Output is correct
17 Correct 7 ms 6600 KB Output is correct
18 Correct 5 ms 6596 KB Output is correct
19 Correct 4 ms 6476 KB Output is correct
20 Correct 4 ms 6476 KB Output is correct
21 Correct 7 ms 8264 KB Output is correct
22 Correct 5 ms 6604 KB Output is correct
23 Correct 5 ms 6600 KB Output is correct
24 Correct 7 ms 6604 KB Output is correct
25 Correct 4 ms 6604 KB Output is correct
26 Correct 4 ms 6604 KB Output is correct
27 Correct 5 ms 6604 KB Output is correct
28 Correct 5 ms 6604 KB Output is correct
29 Correct 5 ms 6604 KB Output is correct
30 Correct 5 ms 6604 KB Output is correct
31 Correct 5 ms 6604 KB Output is correct
32 Correct 7 ms 6604 KB Output is correct
33 Correct 5 ms 6596 KB Output is correct
34 Correct 7 ms 6600 KB Output is correct
35 Correct 5 ms 6604 KB Output is correct
36 Correct 5 ms 6608 KB Output is correct
37 Correct 5 ms 6604 KB Output is correct
38 Correct 5 ms 6604 KB Output is correct
39 Correct 5 ms 6604 KB Output is correct
40 Correct 5 ms 6604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6596 KB Output is correct
2 Correct 5 ms 6596 KB Output is correct
3 Correct 4 ms 6476 KB Output is correct
4 Correct 4 ms 6476 KB Output is correct
5 Correct 5 ms 6604 KB Output is correct
6 Correct 7 ms 6476 KB Output is correct
7 Correct 5 ms 6596 KB Output is correct
8 Correct 5 ms 6476 KB Output is correct
9 Correct 7 ms 6604 KB Output is correct
10 Correct 4 ms 6596 KB Output is correct
11 Correct 5 ms 6476 KB Output is correct
12 Correct 4 ms 6604 KB Output is correct
13 Correct 4 ms 6604 KB Output is correct
14 Correct 5 ms 6600 KB Output is correct
15 Correct 4 ms 6476 KB Output is correct
16 Correct 5 ms 6476 KB Output is correct
17 Correct 7 ms 6600 KB Output is correct
18 Correct 5 ms 6596 KB Output is correct
19 Correct 4 ms 6476 KB Output is correct
20 Correct 4 ms 6476 KB Output is correct
21 Correct 7 ms 8264 KB Output is correct
22 Correct 5 ms 6604 KB Output is correct
23 Correct 5 ms 6600 KB Output is correct
24 Correct 7 ms 6604 KB Output is correct
25 Correct 4 ms 6604 KB Output is correct
26 Correct 4 ms 6604 KB Output is correct
27 Correct 5 ms 6604 KB Output is correct
28 Correct 5 ms 6604 KB Output is correct
29 Correct 5 ms 6604 KB Output is correct
30 Correct 5 ms 6604 KB Output is correct
31 Correct 5 ms 6604 KB Output is correct
32 Correct 7 ms 6604 KB Output is correct
33 Correct 5 ms 6596 KB Output is correct
34 Correct 7 ms 6600 KB Output is correct
35 Correct 5 ms 6604 KB Output is correct
36 Correct 5 ms 6608 KB Output is correct
37 Correct 5 ms 6604 KB Output is correct
38 Correct 5 ms 6604 KB Output is correct
39 Correct 5 ms 6604 KB Output is correct
40 Correct 5 ms 6604 KB Output is correct
41 Correct 49 ms 13644 KB Output is correct
42 Correct 37 ms 10296 KB Output is correct
43 Correct 40 ms 10380 KB Output is correct
44 Correct 44 ms 10336 KB Output is correct
45 Correct 36 ms 10412 KB Output is correct
46 Correct 58 ms 14092 KB Output is correct
47 Correct 85 ms 14136 KB Output is correct
48 Correct 80 ms 14056 KB Output is correct
49 Correct 72 ms 14140 KB Output is correct
50 Correct 33 ms 10172 KB Output is correct
51 Correct 26 ms 10184 KB Output is correct
52 Correct 34 ms 10192 KB Output is correct
53 Correct 57 ms 13668 KB Output is correct
54 Correct 50 ms 13760 KB Output is correct
55 Correct 51 ms 13720 KB Output is correct
56 Correct 5 ms 6732 KB Output is correct
57 Correct 11 ms 9688 KB Output is correct
58 Correct 52 ms 15528 KB Output is correct
59 Correct 115 ms 23472 KB Output is correct
60 Correct 49 ms 13812 KB Output is correct
61 Correct 131 ms 13820 KB Output is correct