Codeforces 931D Peculiar apple-tree(dfs+思維).cpp

來源:互聯網
上載者:User

標籤:amp   color   .com   contest   clu   匯聚   force   vector   ios   

題目連結:http://codeforces.com/contest/931/problem/D

題目大意:給你一顆樹,每個節點都會長蘋果,然後每一秒鐘,蘋果往下滾一個。兩個兩個會抵消蘋果。問最後在根節點能收到多少個蘋果。

解題思路:昨天是我想複雜了,其實就是統計下每層的蘋果數,若是奇數則答案+1。因為最終這些蘋果都會滾落匯聚到根節點,所以在滾落過程中肯定會碰撞並相消無論蘋果是怎麼分布的。

代碼:

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<algorithm> 6 using namespace std; 7 const int N=1e5+5; 8  9 int dep_num[N];10 vector<int>v[N];11 12 void dfs(int u,int dep){13     dep_num[dep]++;14     for(int i=0;i<v[u].size();i++){15         int t=v[u][i];16         dfs(t,dep+1);17     }18 }19 20 int main(){21     int n;22     scanf("%d",&n);23     for(int i=2;i<=n;i++){24         int fa;25         scanf("%d",&fa);26         v[fa].push_back(i);27     }28     dfs(1,1);29     int ans=0;30     for(int i=0;i<N;i++){31         ans+=dep_num[i]%2;32     }33     printf("%d\n",ans);34     return 0;35 }

 

Codeforces 931D Peculiar apple-tree(dfs+思維).cpp

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.