이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* Author : Tr3nity */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod1 (1000000000+7)
#define mod (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp make_pair
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 18;
struct dp{
int val, dist, next;
};
int n, d;
vector<int> g[N];
dp a[N];
void update(int v, int x, int dep, int nxt){
if(x > a[v].val){
a[v].val = x;
a[v].dist = dep;
a[v].next = nxt;
}else if(x == a[v].val && dep > a[v].dist){
a[v].dist = dep;
a[v].next = nxt;
}
}
void dfs(int v){
int sum = 0, small = 0, BS = -1, SB = d, NS = d;
for(int u: g[v]){
dfs(u);
sum += a[u].val;
if(a[u].dist < (d+1)/2){
small++;
if(BS == -1 || a[BS].dist < a[u].dist){
BS = u;
}
if(SB > a[u].next)
NS = SB, SB = a[u].next;
else if(NS > a[u].next)
NS = a[u].next;
}else if(d % 2 == 0 && a[u].dist == d / 2){
BS = u;
}
if((d+1)/2 <= a[u].dist){
if(SB > a[u].next)
NS = SB, SB = a[u].next;
else if(NS > a[u].next)
NS = a[u].next;
if(SB > a[u].dist)
NS = SB, SB = a[u].dist;
else if(NS > a[u].dist)
NS = a[u].dist;
}
}
if(g[v].size() == 0){
a[v].val = 1, a[v].dist = 1;
return;
}
bool put = SB == d;
update(v, sum - small + put, (put ? 0 : SB), (put ? d : NS));
if(BS != -1 && a[BS].dist + SB >= d){
if(d % 2 == 0 && a[BS].dist == d/2){
update(v, sum - small, d/2, NS);
}else{
update(v, sum - small + 1, a[BS].dist, SB);
}
}
a[v].dist++;
a[v].next++;
}
void solve(){
cin >> n >> d;
for(int i = 1; i <= n-1; ++i){
int x; cin >> x;
g[x].pb(i);
}
for(int i = 0; i < n; ++i) a[i].val = 0, a[i].dist = d, a[i].next = d;
dfs(0);
cout << a[0].val;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;aa=tt;
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
catinatree.cpp: In function 'int main()':
catinatree.cpp:93:15: warning: unused variable 'aa' [-Wunused-variable]
93 | int tt = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |