CF482D Random Function and Tree 树形DP + 思维 + 神题

Code:

#include<bits/stdc++.h>
#define ull unsigned long long
#define MOD 1000000007
#define ll long long
#define maxn 120000
using namespace std;
void setIO(string s)
{
	string in=s+".in";
	string out=s+".out";
	freopen(in.c_str(),"r",stdin);
	// freopen(out.c_str(),"w",stdout);
}
vector<int>G[maxn];
ll F[maxn][2];
void solve(int n)
{
	for(int u=n;u>=1;--u)
	{
		F[u][1]=1,F[u][0]=0;
		int siz=G[u].size();
		if(siz==0) continue;
		for(int i=0;i<siz;++i)
		{
			int v=G[u][i];
			ll a = ((F[u][0]*F[v][0])%MOD+(F[u][1]*F[v][1])%MOD)%MOD;
			ll b = ((F[u][1]*F[v][0])%MOD+(F[u][0]*F[v][1])%MOD)%MOD;
			F[u][0]=(F[u][0]+a)%MOD;
			F[u][1]=(F[u][1]+b)%MOD;
		}
		F[u][0]=(F[u][0]*2ll)%MOD;
		F[u][1]=(F[u][1]*2ll)%MOD;
		ll p[3],tmp;
		p[0]=p[1]=1, p[2]=0;
		for(int i=0;i<siz;++i)
		{
			int v=G[u][i];
			p[0]=((p[0]*F[v][0])%MOD+p[0])%MOD;         // 全偶 (即左右走都一样)
			tmp=p[2];
			p[2]=((p[1]*F[v][1])%MOD+p[2])%MOD;
			p[1]=((tmp*F[v][1])%MOD+p[1])%MOD;
		}
		F[u][1]=(F[u][1]-p[0]+MOD)%MOD;
		F[u][0]=(F[u][0]-p[2]+MOD)%MOD;
	}
}
int main()
{
	// setIO("input");
	int n;
	scanf("%d",&n);
	for(int i=2;i<=n;++i)
	{
		int p;
		scanf("%d",&p);
		G[p].push_back(i);
	}
	solve(n);
	printf("%lld\n",(F[1][0]+F[1][1])%MOD);
	return 0;
}

  

原文地址:https://www.cnblogs.com/guangheli/p/11047174.html

时间: 2024-08-30 10:31:11

CF482D Random Function and Tree 树形DP + 思维 + 神题的相关文章

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

codeforces161D - Distance in Tree 树形dp

题意:给你一棵树,问你树中距离为k的有多少种情况. 解题思路:树形dp  维护每个节点(1-K)深度的情况, 解题代码: 1 // File Name: 161d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 19时20分10秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #incl

hdu5593/ZYB&#39;s Tree 树形dp

ZYB's Tree Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一颗NN个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采用如下方式进行读入输出: 读入:读入两个数A,BA,B,令fa_ifa?i??为节点ii的父亲,fa_1=0fa?1??=0;fa_i=(A*i+B)\%(i-1)+1fa

URAL_1018 Binary Apple Tree 树形DP+背包

这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #

Codeforces 461B Appleman and Tree(树形dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每个节点的父亲节点,以及每个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量,保证每个联通分量有且仅有1个黑色节点.问有多少种分割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的分割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include <c

CF 461B Appleman and Tree 树形DP

Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then

hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定义每条路径的值为经过的节点的不同颜色数.求所有路径的值和. 思路:看题解后,才想出来的.树形dp. 求所有路径的值和 = 路径条数*总颜色数(n*(n-1)*colors/2)-sigma(每种颜色没有经过的路径条数) 主要是求每种颜色没有经过的路径条数. 画一棵树,我直接用颜色值表示节点编号. 2

URAL 1018 Binary Apple Tree 树形DP 好题 经典

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

Codeforces 462D Appleman and Tree 树形dp

题目链接:点击打开链接 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 思路: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. #include <cstdio> #include <vector> #include <iostream> using namespace std; #de