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基础之列表、字典、元祖等 (二)
一.作用域 if 1==1: name = 'weibinf' print name 下面的结论对吗? 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 result = 值1 ...
- 查看MySQL日志数据binlog文件
binlog介绍 binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格式 ...
- libcurl在mingw下编译
通过命令提示符进入 curl-7.27.0 文件夹输入 mingw32-make mingw32 进行生成(这里我只需要普通的功能,于是没有加附加的选项)编译完成后,在 lib 文件夹中会有我们需要的 ...
- Google Map API 应用实例说明
目录 Google Map API 1基础知识 1.1 Google 地图 API 概念 1.2 Google 地图的"Hello, World" 1.2.1 加载 Google ...
- 2017北京国庆刷题Day6 afternoon
期望得分:100+100+40=240 实际得分:100+0+40=140 二进制拆分.二进制前缀和 #include<cstdio> #include<iostream> u ...
- 关于 Capella 需要纠正的语音
li { font-size: 18px; } 关于 Capella 需要纠正的语音 持续更新 浊塞音声带要振动 区分 [θ]/[ð] 和 [t̪],注意舌位 [ɫ] 的舌位,切记不能圆唇 [æ] 的 ...
- spring boot 使用logback日志系统的详细说明
springboot按照profile进行打印日志 log4j logback slf4j区别? 首先谈到日志,我们可能听过log4j logback slf4j这三个名词,那么它们之间的关系是怎么样 ...
- Hibernate + mysql 查询伪劣时:= 出现 Space is not allowed after parameter prefix ':' MySQL异常
需求: 要求查询一个站点在地市和省的排名信息出来. 效果图: 代码实现: 注释的部分是起初采用的hibernate 查询,但无论怎么写都会报 space is not allowed after pa ...
- React-music 全家桶项目
React-Music 全家桶项目 一.简介 该项目是基于React全家桶开发的一个音乐播放器,技术栈采用:Webpack + React + React-redux + React-router + ...
- 【BZOJ】2337: [HNOI2011]XOR和路径 期望+高斯消元
[题意]给定n个点m条边的带边权无向连通图(有重边和自环),在每个点随机向周围走一步,求1到n的期望路径异或值.n<=100,wi<=10^9. [算法]期望+高斯消元 [题解]首先异或不 ...