您的位置:首页 > 移动开发

CodeForces - 931D Peculiar apple-tree(思维)

2018-03-09 12:31 537 查看
Peculiar apple-treeIn Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence theyannihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.InputFirst line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences.Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.OutputSingle line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.题意:树的每个花的下面都连有其他的花(输入中给出),结了果子会沿着花一层层的下落,每两个果子相遇都会撞碎,所有花上的果子从第1秒开始同时下落,问可以从底部接到多少果子.
思路:脑子瓦特了,就是想不起来怎么做,每层的苹果都会同时下落,他们早晚会第一层相遇,也就是每一层最多只剩下一个果子,只需算一下每一层果子个数的奇偶性就可以.
代码:#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;

int n;
int sum[maxn],deep[maxn];//sum每一层果子个数,deep层数

int main()
{
cin>>n;
deep[1] = 1;
for(int i = 2;i<= n;i++)
{
int x;
scanf("%d",&x);
deep[i] = deep[x]+1;
sum[deep[i]]++;
}

int ans = 0;
for(int i = 1;i<= n;i++)
ans+= sum[i]%2;
cout<<ans+1<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  思维 codeforces