题目大意:给定一个无根树,给每条边黑白染色,求出每个点为根时,其他点到根的路径上至多有一条黑边的染色方案数,模$1e9+7$。

题解:树形$DP$不难想到,记$f_u$为以$1$为根时,以$u$为根的子树的方案数,$f_u=\prod\limits_{v\in son_u}(f_v+1)$

换根也很简单。

但是这题卡模数,换根时要求逆元,其中$f_u$可能等于$1e9+6$,加一后变成$0$,无法求逆。可以求前缀积和后缀积转移

卡点:原$dp$写错

C++ Code:

#include <cstdio>
#include <vector>
#include <cctype>
namespace R {
int x, ch;
inline int read() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
}
using R::read; #define maxn 200010
const int mod = 1e9 + 7;
int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxn << 1];
inline void add(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
} int n;
int f[maxn], g[maxn], ans[maxn], l[maxn], r[maxn];
void dfs(int u, int fa = 0) {
f[u] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa) {
dfs(v, u);
f[u] = static_cast<long long> (1 + f[v]) * f[u] % mod;
}
}
}
void dfs1(int u, int fa = 0) {
std::vector<int> S;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa) S.push_back(v);
}
if (S.size()) {
l[*S.begin()] = g[u];
for (std::vector<int>::iterator it = S.begin() + 1; it != S.end(); it++) {
l[*it] = static_cast<long long> (l[*(it - 1)]) * (f[*(it - 1)] + 1) % mod;
}
r[*(S.end() - 1)] = 1;
if (S.begin() + 1 != S.end()) {
for (std::vector<int>::iterator it = S.end() - 2; true; it--) {
r[*it] = static_cast<long long> (r[*(it + 1)]) * (f[*(it + 1)] + 1) % mod;
if (it == S.begin()) break;
}
}
}
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa) {
g[v] = (static_cast<long long> (l[v]) * r[v] + 1) % mod;
ans[v] = static_cast<long long> (g[v]) * f[v] % mod;
dfs1(v, u);
}
}
} int main() {
n = read();
for (int i = 1, x; i < n; i++) {
x = read();
add(i + 1, x);
add(x, i + 1);
}
dfs(1);
ans[1] = f[1]; g[1] = 1;
dfs1(1);
for (int i = 1; i <= n; i++) {
printf("%d", ans[i]);
putchar(i == n ? '\n' : ' ');
}
return 0;
}

  

[CF543D]Road Improvement的更多相关文章

  1. VK Cup 2016 - Qualification Round 2 C. Road Improvement dfs

    C. Road Improvement 题目连接: http://www.codeforces.com/contest/638/problem/C Description In Berland the ...

  2. Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp

    D - Road Improvemen 思路:0没有逆元!!!! 不能直接除,要求前缀积和后缀积!!! #include<bits/stdc++.h> #define LL long lo ...

  3. Codeforces 543D Road Improvement(树形DP + 乘法逆元)

    题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...

  4. Codeforces 543D. Road Improvement (树dp + 乘法逆元)

    题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...

  5. Codeforces 543D Road Improvement(DP)

    题目链接 Solution 比较明显的树形DP模型. 首先可以先用一次DFS求出以1为根时,sum[i](以i为子树的根时,满足要求的子树的个数). 考虑将根从i变换到它的儿子j时,sum[i]产生的 ...

  6. Codeforces 543D Road Improvement

    http://codeforces.com/contest/543/problem/D 题意: 给定n个点的树 问: 一开始全是黑边,对于以i为根时,把树边白染色,使得任意点走到根的路径上不超过一条黑 ...

  7. CodeForces 543D:Road Improvement

    题目:http://codeforces.com/problemset/problem/543/D 题意:给你一棵树,一开始边都是0,可以使任意的边变成1,对于每一个根节点求使得它到其他任一点的路径上 ...

  8. [Codeforces543D]Road Improvement

    Problem 刚开始每条边都是坏的,现在要选取一个点使得其他点到这个点的路径上最多只有一条坏路,问至少要修好多少条边 Solution 如果以1为根,那么是个简单的树形DP 设根从u转移到v,那么u ...

  9. CodeForces 543D 树形DP Road Improvement

    题意: 有一颗树,每条边是好边或者是坏边,对于一个节点为x,如果任意一个点到x的路径上的坏边不超过1条,那么这样的方案是合法的,求所有合法的方案数. 对于n个所有可能的x,输出n个答案. 分析: 题解 ...

随机推荐

  1. android 自定义图片圆形进度条

    感觉话一个圆形进度条挺简单的 ,但是却偏偏给了几张图片让你话,说实话我没接触过,感觉好难,还好百度有大把的资源,一番努力下终于画出来了. 代码如下. package com.etong.cpms.wi ...

  2. MySql外键建立在哪里(更新)

    一对一的时候:分为主表和附表  外键建立在附件上  附表的外键关联到主表的主键上,Example:学生表和学生信息表,在学生信息表上建立外键 一对多的时候:分为一和多  外键建立在多上  Exampl ...

  3. (原创)用Verilog实现一个参数化的呼吸灯(Verilog,CPLD/FPGA)

    1.Abstract     观察到一个有趣的现象,每当把Apple笔记本合上的时候,那个白色的呼吸灯就会反复地由暗渐明,然后又由明渐暗,乍一看就像Apple笔记本在打盹休息一样,十分可爱!于是突发奇 ...

  4. UVA10474 Where is the Marble?【排序】

    参考:https://blog.csdn.net/q547550831/article/details/51326321 #include <iostream> #include < ...

  5. 实现一个带有指纹加密功能的笔记本(Android)第一部分

    自己经常会忘记一些密码什么的,想把这些密码保存下来,但是别人做的软件总有一点不安全的感觉,所以自己动手做了一个带有指纹加密的笔记本. 以下是本工程用到的一些第三方包 compile 'org.gree ...

  6. JavaScript---设计模式之迭代器模式

    迭代器模式提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该方法中的内部表示. jQuery中我们经常会用到一个each函数就是迭代器模式 作用 为遍历不同的集合结构提供一个统一的接口,从而 ...

  7. 4-oracle11g安装

    1.导入安装包,解压 [root@ocp Desktop]# unzip p10404530_112030_Linux-x86-64_1of7.zip [root@ocp Desktop]# unzi ...

  8. 什么鬼,又不知道怎么命名class了

    什么鬼,又不知道怎么命名class了 2015/10/25 · CSS · class 分享到:5 原文出处: 结一(@结一w3cplus)    相信写css的人都会遇到下面的问题: 糟糕,怎么命名 ...

  9. JDBC剖析篇(2):JDBC之PreparedStatement

    一次有人问我为什么要使用JDBC中的PreparedStatement,我说可以“防止SQL注入”,其他的却不能说出个一二三,现在来看看其中的秘密 参考文章: http://www.jb51.net/ ...

  10. es6严格模式需要注意的地方

    1.块级函数 "use strict"; if (true) { function f() { } // 语法错误 } es5中严格模式下禁止声明块级函数,而在es6的严格模式中可 ...