题意:给你一个树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个。两个两个会抵消苹果。问最后在根节点能收到多少个苹果。
解题思路:把树画出来,分析一下就会发现,只需要统计每一层有多少个苹果即可。因为每一层的苹果最后都会汇到根节点,所以直接判断每一层的苹果树是不是2的倍数即可。
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <memory.h>
#include <bitset>
#include <map>
#include <deque>
#include <math.h>
#include <stdio.h>
using namespace std;
using namespace std;
typedef long long int ll;
const int MAXN = 1005;
vector<int> child[200000];
int ceng[200000];
void dfs(int v,int ii){
ceng[ii]++;
for(int i=0;i<child[v].size();i++){
dfs(child[v][i],ii+1);
}
}
int main()
{
int n;
scanf("%d",&n);
int temp;
for(int i=2;i<=n;i++){
scanf("%d",&temp);
child[temp].push_back(i);
}
int ans=0;
dfs(1,1);
for(int i=1;i<=n;i++){
if(ceng[i]%(2)==0)
ceng[i]=0;
if(ceng[i])
ceng[i]=1;
ans+=ceng[i];
}
cout<<ans<<endl;
return 0;
}