D - Road Improvemen

思路:0没有逆元!!!! 不能直接除,要求前缀积和后缀积!!!

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n;
LL dp[N], ans[N], fdp[N];
vector<int> edge[N];
vector<int> L[N], R[N]; void dfs(int u, int fa) {
dp[u] = ;
LL pre = ;
L[u].resize(edge[u].size());
R[u].resize(edge[u].size());
for(int i = ; i < edge[u].size(); i++) {
int v = edge[u][i];
L[u][i] = pre;
if(v == fa) continue;
dfs(v, u);
dp[u] = dp[u] * (dp[v] + ) % mod;
L[u][i] = L[u][i] * (dp[v] + ) % mod;
pre = L[u][i];
} pre = ;
for(int i = edge[u].size() - ; i >= ; i--) {
int v = edge[u][i];
R[u][i] = pre;
if(v == fa) continue;
R[u][i] = R[u][i] * (dp[v] + ) % mod;
pre = R[u][i];
}
} void dfs3(int u, int fa, LL val) {
ans[u] = dp[u] * (val + ) % mod;
for(int i = ; i < edge[u].size(); i++) {
int v = edge[u][i];
if(v == fa) continue;
LL nxv = ;
if(i) nxv = L[u][i - ];
if(i + < edge[u].size()) nxv = nxv * R[u][i + ] % mod;
dfs3(v, u, nxv * (val + ) % mod);
}
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
int x; scanf("%d", &x);
edge[i].push_back(x);
edge[x].push_back(i);
}
dfs(, );
dfs3(, , );
for(int i = ; i <= n; i++) printf("%lld ", ans[i]);
return ;
} /*
*/

Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp的更多相关文章

  1. Codeforces Round #302 (Div. 2) C. Writing Code 简单dp

    C. Writing Code Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/prob ...

  2. CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)

    https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...

  3. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  4. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  5. 完全背包 Codeforces Round #302 (Div. 2) C Writing Code

    题目传送门 /* 题意:n个程序员,每个人每行写a[i]个bug,现在写m行,最多出现b个bug,问可能的方案有几个 完全背包:dp[i][j][k] 表示i个人,j行,k个bug dp[0][0][ ...

  6. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...

  7. 水题 Codeforces Round #302 (Div. 2) A Set of Strings

    题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...

  8. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  9. Codeforces Round #302 (Div. 1)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. Writing Code Programmers working on a ...

随机推荐

  1. Windows 2008 R2上配置IIS7或IIS7.5中的URLRewrite(URL重写)实例

    1. 安装URL Rewrite模块 下载页面 re_write_x86_zh_CN.msi from microsoft re_write_x64_zh_CN.msi from microsoft安 ...

  2. tf.session.run()单函数运行和多函数运行区别

    tf.session.run()单函数运行和多函数运行区别 觉得有用的话,欢迎一起讨论相互学习~Follow Me problem instruction sess.run([a,b]) # (1)同 ...

  3. [LeetCode] 31. Next Permutation ☆☆☆

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. HTML+CSS基础小笔记再整理

    1. font的两个必须要写的:font-size 和 font-family text-indent 首行缩进(em)1em=一个文字大小 text-algin 对齐方式:left.center.r ...

  5. Dozer 对象的"搬运工"

    前言:项目中,经常会遇到各层对象之间相互进行值传递的过程,如在数据据持久层有一持久类EntityA,在视图层可能会变为ViewA,通常情况下,这两个类的属性 名称 .类型都是一致的,   在两个对象传 ...

  6. CF544 C 背包 DP

    n个人写m行代码,第i人写一行代码有a[i]个bug,问总bug数不超过b的不同方案数. 其实就是个背包,dp[i][j][k]代表前i个人写了j行代码用了k个bug限度,然后随便转移一下就好了 /* ...

  7. yum源的使用

    yum通过仓库拉取,同时解决了依赖的问题.有仓库的都是通过社区来维护的,不同的发行版会有不同的社区来维护 此时就是客户端和服务器的关系的问题了,yum会依赖一个配置文件, yum 的理念是使用一个中心 ...

  8. ClassNotFoundException:com.sun.xml.bind.v2.ContextFactory

    项目中引入hive-jdbc-1.2.1-standalone.jar包之后,报错如下: Caused by: javax.xml.bind.JAXBException: Provider com.s ...

  9. thinkphp学习,蛋疼啊~新公司要弄这个。。。

    common/function.php 看里面函数大全,啥玩意都有 Tpl中的是模板文件,action中是控制类,Model中有action用到的一些东西,例: class IndexAction e ...

  10. 【总结】对FFT的理解 / 【洛谷 P3803】 【模板】多项式乘法(FFT)

    题目链接 \(\Huge\text{无图,慎入}\) \(FFT\)即快速傅里叶变换,用于加速多项式乘法. 如果暴力做卷积的话就是一个多项式的每个单项式去乘另一个多项式然后加起来,时间复杂度为\(O( ...