이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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});
}
//cout << se.size() << "\n";
//if(se.size()) cout << ((*se.begin())).fi << "\n";
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});
}
}
//cout << dp[u] << " " << mx << "\n";
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;
}
//cout << dp[u] << "\n";
if(small[u].fi >= d){
dp[u]++;
small[u].fi = 0, small[u].se = d;
}
//cout << u << " " << dp[u] << " " << small[u].fi << " " << small[u].se << "\n";
}
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);
//freopen("cat.inp", "r", stdin);
//freopen("cat.out", "w", stdout);
process();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |