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

Codeforces Round #468 D. Peculiar apple-tree[图DFS+思维]

2018-03-05 16:00 447 查看
D. Peculiar apple-tree

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input

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.

Output

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.

Examples

input

Copy

3
1 1


output
1


input

Copy

5
1 2 2 2


output
3


input

Copy

18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4


output
4


Note

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,代表n个节点,现在知道每个节点的有一个苹果.每隔一秒,苹果会滚落到下一个节点(往root走)如果一个节点有多个苹果,那么2个苹果一组会泯灭.问,最后能获得多少个苹果?

思路:比赛的时候T13了,向模拟时间做.o(n^2)挂了,不要尝试cf不出极限数据

正解: 对于同一深度的苹果,会同时往root走,过程中如果先碰撞over了和最后check在root处判断个数结果是一样.

#include <bits/stdc++.h>
#define MOD 10000
#define INF 0x3f3f3f3f
#define bug cout << "bug" << endl
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;
typedef long long ll;

const int MAX_N=1e5+5;
int n,m;
vector <int> edge[MAX_N];
int cnt[MAX_N],depth[MAX_N];

void dfs1(int v,int dep){
depth[v]=dep;
for(int i=0;i<(int)edge[v].size();i++){
dfs1(edge[v][i],dep+1);
}
return ;
}

//int dfs(int v,int dep,int c){
//    if(dep==depmax-c+1)   return 0;
//    if(edge[v].size()==0){
//        int x=cnt[v];
//        cnt[v]=0;
//        return x;
//    }
//    cnt[v]=0;
//    for(int i=0;i<(int)edge[v].size();i++){
//        cnt[v]+=cnt[edge[v][i]];
//        cnt[edge[v][i]]=0;
//    }
//    cnt[v]%=2;
//    for(int i=0;i<(int)edge[v].size();i++){
//        dfs(edge[v][i],dep+1,c);
//    }
//}

int main(void){
int n,par;
cin >> n;
for(int i=2;i<=n;++i){
scanf("%d",&par);
edge[par].push_back(i);
}
int ans=0;
dfs1(1,0);
for(int i=1;i<=n;i++)   cnt[depth[i]]++;
for(int i=0;i<=n;i++){
if(cnt[i]&1)   ans++;
}
cout << ans << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces dfs