In 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 they annihilate. 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.
First 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.
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
3 1 1
1
5 1 2 2 2
3
18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
4
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
题意
给你一个数字代表有n个苹果 苹果只有在节点为1的地方才能被收集 其他所有的苹果会在每秒钟同时往下走 第i个苹果能也只能往pi个点走 pi(2 - n)已经给你了
思路
刚开始我的方法是模拟掉落的过程 , 然后写的时候就想到了有一种1 2 3 4 5 。。。 递增的序列能够让这种写法的复杂度为O(n^2) 果然TLE
看了别人的解法 , 比较简单的解法是 距离根部为k的所有果实我们假设他们不会再运输的途中被消耗掉任意一个,那么他们一定会在1号节点被消耗 所以只需要看他们的数量是奇数还是偶数就好了 而这个判断距离为k的果实有多少个可以用DFS或者BFS来判断
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
vector <int> vec[N];
int index[N];
void dfs(int x , int deep)
{
index[deep] ++;
for (auto iter : vec[x])
dfs(iter , deep + 1);
}
int main()
{
int n , temp;
while (~scanf("%d",&n)) {
//vec.clear();
memset(index , 0 , sizeof(index));
for (int i = 2 ; i <= n ; i ++) {
scanf("%d",&temp);
vec[temp].push_back(i);
}
dfs(1 , 1);
int res = 0;
for (int i = 1 ; i <= n ; i ++)
if (index[i] & 1)
res ++;
printf("%d\n",res);
}
}
/*
人生如此复杂,机会多得像稠密图,我们没理由认输。尽管我们走不了最短路,但图仍是连通图,TLE之前,没有一个节点叫失败。
*/