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 ...
随机推荐
- python中如何优雅使用import
http://note.youdao.com/noteshare?id=c55be6a8565f5eb586aa52244b3af010
- libcurl在mingw下编译
通过命令提示符进入 curl-7.27.0 文件夹输入 mingw32-make mingw32 进行生成(这里我只需要普通的功能,于是没有加附加的选项)编译完成后,在 lib 文件夹中会有我们需要的 ...
- OpenCV---轮廓发现
推文:OpenCV-Python教程(11.轮廓检测) 轮廓发现 是基于图像边缘提取的基础,寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终轮廓的发现 相关API findContours 发现轮 ...
- mysql \G
mysql 命令区分大小写.ego (\G) Send command to mysql server, display result vertically. go (\g) ...
- Jenkins使用教程之创建job
第一部分:常规设置(general) 1.点击新建,进入新建项目页面 2.输入项目名称,选择构建一个自由风格的软件项目(里面可以自己自由进行配置,使用更加灵活),点击ok 3.项目名称与描述 4.gi ...
- HDU6127 简单几何 暴力二分
LINK 题意:给出n个点,每个点有个权值,可以和任意另外一点构成线段,值为权值积.现问过原点的直线中交所有线段的权值和的最大值,注意直线必不经过点. 思路:直线可以将点集分为两侧,此时的权值为两侧点 ...
- /i,/m,/s,/x,/A,/s,/U,/x,/j,/u 等正则修饰符用法~
i (PCRE_CASELESS) 如果设置了这个修饰符,模式中的字母会进行大小写不敏感匹配. m (PCRE_MULTILINE) 默认情况下,PCRE 认为目标字符串是由单行字符组成的(然而实际上 ...
- HDU 1166 敌兵布阵 (树状数组 单点修改+区间查询)
题目链接 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和T ...
- NYOJ 163 Phone List (字符串处理 字典树)
题目链接 描述 Given a list of phone numbers, determine if it is consistent in the sense that no number is ...
- python操作YAML文件之pyyaml库
1. YAML简介 YAML是一种被认为可以超越XML.JSON的配置文件,最早接触是Spring Boot,木有想到python也是支持的,遂研究一下. python解析YAML库叫做pyyaml, ...