Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp
思路: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的更多相关文章
- 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 ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)
https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- 完全背包 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][ ...
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...
- 水题 Codeforces Round #302 (Div. 2) A Set of Strings
题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #302 (Div. 1)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. Writing Code Programmers working on a ...
随机推荐
- springMVC参数的获取区别
在springMVC中我们一般使用注解的形式来完成web项目,但是如果不明白springmvc的对于不同注解的应用场景就会很容易犯错误 1.什么是restful形式: 什么是RESTful restf ...
- STL源码分析-priority_queue
http://note.youdao.com/noteshare?id=402cd1487d34af1a11f0daac6d16132f
- git 从新的git 库中拉取---变换git地址用;
2.先删后加 git remote rm origin git remote add origin [url]----- example : git remote add origin http: ...
- Android Studio获取调试版SHA1和发布版SHA1的方法
前言: 当我们在集成高德地图定位功能到项目中的时候,需要用到发布版和调试版的安全码SHA1,所以今天我就来总结一下分别在Windows和Mac下是怎么来获取SHA1的,希望对大家有所帮助. 首先,统一 ...
- iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2014年2月19日更新版)
//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496069.html 编号 iOS-Apple苹果官方文档翻译名称 博文链接地址 ...
- 在Unity中实现屏幕空间阴影(2)
参考文章: https://www.imgtec.com/blog/implementing-fast-ray-traced-soft-shadows-in-a-game-engine/ 完成的工程: ...
- 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)
题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...
- 一款线程安全、基本功能齐全的STL
MiniSTL 目前正在完成一个STL,主要想通过该项目锻炼C++编程.模板编程.熟悉STL.锻炼数据结构和算法能力. 项目的目标是实现STL的几大构件+线程安全.项目过程中主要参考SGI STL源码 ...
- 关于linux系统如何实现fork的研究(二)【转】
转自:http://www.aichengxu.com/linux/7166015.htm 本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 前一篇关于li ...
- ubuntu新机安装工具
ubuntu新机安装工具:1,sudo apt-get install ssh vim2, 设置root密码,以备不时之需: 执行:sudo passwd root 然后输入当前三次密码,第一次是当前 ...