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;
}
思路:脑子瓦特了,就是想不起来怎么做,每层的苹果都会同时下落,他们早晚会第一层相遇,也就是每一层最多只剩下一个果子,只需算一下每一层果子个数的奇偶性就可以.
代码:#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 430C Xor-tree【Dfs+思维】
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree - Codeforces(思维)
- codeforces 764C Timofey and a tree (思维题)
- Codeforces 699D. Fix a Tree【并查集+思维】
- codeforces 764C Timofey and a tree(树+思维)
- CodeForces - 902B Coloring a Tree 思维
- Codeforces 682C Alyona and the Tree【Dfs+YY思维】好题
- Codeforces 764C Timofey and a tree 树+思维
- CodeForces - 711C Coloring Trees(DP)(思维)
- Codeforces 574B Bear and Three Musketeers【思维】
- Codeforces 369C Valera and Elections【思维+Dfs】
- Codeforces 763B Timofey and rectangles(四色定理)(思维)
- CodeForces 620 E.New Year Tree(线段树)
- Codeforces 690F1 - Tree of Life (easy)
- *Wet Shark and Bishops(思维) CodeForces - 621B
- Codeforces 789A Anastasia and pebbles(数学,思维题)
- Imbalance Value of a Tree (CodeForces 915F)
- 【Codeforces 752 C New Year and Rating】+ 思维
- 【27.48%】【codeforces 699D】 Fix a Tree
- 山东省第五届省赛 F - Full Binary Tree (利用满二叉树性质的一个思维题)