Title Link: Http://codeforces.com/contest/931/problem/D
The main topic: give you a tree, each node will grow apples, and then every second, apple roll down one. Two of the two will counteract the apples. Ask how many apples can be received at the root node at the end.
Problem-solving ideas: Yesterday is I want to complicate, in fact, the statistics of each layer of the number of apples, if the odd answer +1. Because eventually these apples will tumble and converge to the root node, so in the rolling process will definitely collide and eliminate no matter how the apple is distributed.
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <algorithm>6 using namespacestd;7 Const intn=1e5+5;8 9 intDep_num[n];Tenvector<int>V[n]; One A voidDfsintUintDEP) { -dep_num[dep]++; - for(intI=0; I<v[u].size (); i++){ the intt=V[u][i]; -DFS (t,dep+1); - } - } + - intMain () { + intN; Ascanf"%d",&n); at for(intI=2; i<=n;i++){ - intFA; -scanf"%d",&FA); - V[fa].push_back (i); - } -Dfs1,1); in intans=0; - for(intI=0; i<n;i++){ toans+=dep_num[i]%2; + } -printf"%d\n", ans); the return 0; *}
Codeforces 931D Peculiar Apple-tree (dfs+ thinking). cpp