【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.
題目大意:
? ??一顆蘋果樹,每個(gè)開花的位置都會(huì)結(jié)一個(gè)蘋果,花的位置序列已經(jīng)給出(n各節(jié)點(diǎn)則輸入n-1個(gè)數(shù),因?yàn)槟J(rèn)1號(hào)節(jié)點(diǎn)在深度為0),輸入的順序即是樹的節(jié)點(diǎn)的標(biāo)號(hào)順序,輸入的值代表這個(gè)節(jié)點(diǎn)的父親節(jié)點(diǎn)是的序號(hào),結(jié)果子之后,蘋果會(huì)向下落、序列1的位置在最下面。只有落到序列1位置的蘋果可以摘,蘋果下落時(shí)有兩種規(guī)則
1、兩個(gè)蘋果相撞就會(huì)消失
2、蘋果下一次出現(xiàn)的位置為當(dāng)前花序?qū)?yīng)的pi值
?
問(wèn) 一個(gè)能摘到多少個(gè)蘋果
解題報(bào)告:
? ? ?這個(gè)題的關(guān)鍵是分析出,最后摘到蘋果的數(shù)量就是每一層的果子的奇偶數(shù),而與 ?和上一層的節(jié)點(diǎn)的連接關(guān)系無(wú)關(guān)。 ?即假設(shè):第二層有兩個(gè)節(jié)點(diǎn),第三層有三個(gè)節(jié)點(diǎn),那么,第三層的節(jié)點(diǎn)無(wú)論怎么與第二層的連接,你會(huì)發(fā)現(xiàn)最終落到第一層的結(jié)果都是一樣的(因?yàn)檫@一層的在某一個(gè)時(shí)間內(nèi)同時(shí)落下,與第二層本身有幾個(gè)果子,第四層本身有多少果子都沒(méi)有關(guān)系,所以相當(dāng)于認(rèn)為這棵樹上只有第三層有果子,來(lái)分析到第一層的情況)。
AC代碼:(直接維護(hù)每一層的果子數(shù))
#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代碼:(可以用搜索樹從下到上跑一邊,其本質(zhì)是一樣的都是要統(tǒng)計(jì)每一層果子數(shù))
#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; }總結(jié):思維題還是要先分析一下情況在動(dòng)筆,說(shuō)不定題目很簡(jiǎn)單,但是想復(fù)雜了 !可以找?guī)讉€(gè)樣例找找規(guī)律。
總結(jié)
以上是生活随笔為你收集整理的【CF#468 div2 D. 】Peculiar apple-tree(思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 牛不过三的定律是真的吗?2021年会是牛
- 下一篇: 【CodeForces - 299C 】