Codeforces 461B Appleman and Tree

Source: Internet
Author: User

Test instructions: Ask you to have a black-and-white point without a tree so that there is only one black dot in each subtree number of methods.

Problem-solving idea: Tree-shaped DP,DP[I][0/1] Indicates whether there are 1 kinds of black dots in the Unicom diagram of the node I.

Problem Solving Code:

1 //File name:461c.cpp2 //Author:darkdream3 //Created time:2015 March 11 Wednesday 10:53 22 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - #defineM 1000000007 - using namespacestd; - intcolor[100005]; -LL dp[100005][2]; in intN; -vector<int> mp[100005];  to intDfsintKintLA) + { -      if(Color[k]) thedp[k][1] =1;  *dp[k][0] = dp[k][1]^1; $       for(inti =0; i < mp[k].size (); i + +)Panax Notoginseng      { -          if(Mp[k][i] = =LA) the              Continue; +          inty =Mp[k][i]; A DFS (y,k); theLL T1 = dp[k][0] * (dp[y][0]+dp[y][1]) ; +LL t2 = dp[k][1] * (dp[y][0] + dp[y][1]) + dp[k][0]*dp[y][1]; -dp[k][0] = T1%M; $dp[k][1] = t2%M; $           -      } -       the } - intMain () {Wuyiscanf"%d",&n); the        for(inti =0; i < n-1; i + +) -       { Wu          intp; -scanf"%d",&p); AboutMp[p].push_back (i+1); $mp[i+1].push_back (p); -       } -        for(inti =0; i < N;i + +) -       { Ascanf"%d",&color[i]); +       } theDfs0,0); -printf"%i64d\n", dp[0][1]);  $ return 0; the}
View Code

Codeforces 461B Appleman and Tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.