【CF#468 div2 D. 】Peculiar apple-tree(思维)
題干:
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
3 1 1Output
1Input
5 1 2 2 2Output
3Input
18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4Output
4Note
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-1個數,因為默認1號節點在深度為0),輸入的順序即是樹的節點的標號順序,輸入的值代表這個節點的父親節點是的序號,結果子之后,蘋果會向下落、序列1的位置在最下面。只有落到序列1位置的蘋果可以摘,蘋果下落時有兩種規則
1、兩個蘋果相撞就會消失
2、蘋果下一次出現的位置為當前花序對應的pi值
?
問 一個能摘到多少個蘋果
解題報告:
? ? ?這個題的關鍵是分析出,最后摘到蘋果的數量就是每一層的果子的奇偶數,而與 ?和上一層的節點的連接關系無關。 ?即假設:第二層有兩個節點,第三層有三個節點,那么,第三層的節點無論怎么與第二層的連接,你會發現最終落到第一層的結果都是一樣的(因為這一層的在某一個時間內同時落下,與第二層本身有幾個果子,第四層本身有多少果子都沒有關系,所以相當于認為這棵樹上只有第三層有果子,來分析到第一層的情況)。
AC代碼:(直接維護每一層的果子數)
#include<bits/stdc++.h>using namespace std; int sum[100000 + 5]; int dep[100000 + 5]; int root; int ans; int main() {int n;int maxx = 0;cin>>n;dep[1]=1;sum[1] = 1;for(int i = 2; i<=n; i++) {scanf("%d",&root);dep[i] =dep[root] + 1;maxx = max(maxx,dep[i] );sum[dep[i] ] +=1; } // printf("maxx = %d\n",maxx);for(int i = 1; i<=maxx; i++) {ans +=sum[i]%2;}cout<<ans<<endl;return 0 ; }AC代碼:(可以用搜索樹從下到上跑一邊,其本質是一樣的都是要統計每一層果子數)
#include <bits/stdc++.h> #define MOD 10000 #define INF 0x3f3f3f3f #define bug cout << "bug" << endlusing 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 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; }總結:思維題還是要先分析一下情況在動筆,說不定題目很簡單,但是想復雜了 !可以找幾個樣例找找規律。
總結
以上是生活随笔為你收集整理的【CF#468 div2 D. 】Peculiar apple-tree(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛不过三的定律是真的吗?2021年会是牛
- 下一篇: 可转债怎么买?如何挑选可转债?