# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
172694 | rzbt | Cat in a tree (BOI17_catinatree) | C++14 | 116 ms | 15712 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 200005
typedef long long ll;
using namespace std;
int dp[MAXN],udalj[MAXN];
int n,k;
vector<int> graf[MAXN];
void dfs(int t){
if(graf[t].empty()){
dp[t]=1;
udalj[t]=0;
return;
}
dfs(graf[t][0]);
udalj[t]=udalj[graf[t][0]]+1;
dp[t]=dp[graf[t][0]];
if(udalj[t]==k){
udalj[t]=0;
dp[t]++;
}
bool prvi=true;
for(auto x:graf[t]){
if(prvi){
prvi=false;
continue;
}
dfs(x);
if(udalj[t]+udalj[x]+1>=k){
dp[t]+=dp[x];
udalj[t]=min(udalj[t],udalj[x]+1);
}else{
dp[t]+=dp[x]-1;
udalj[t]=max(udalj[t],udalj[x]+1);
}
}
//printf("%d %d %d",t,udalj[t],dp[t]);
}
int main()
{
scanf("%d %d", &n, &k);
for(int i=1;i<n;i++){
int t;
scanf("%d", &t);
graf[t].pb(i);
}
dfs(0);
printf("%d",dp[0]);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |