题目大意:给定一个无根树,给每条边黑白染色,求出每个点为根时,其他点到根的路径上至多有一条黑边的染色方案数,模$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. react初学

    react和vue一样都是mvvm的这种开发模式. 下载js文件 引入HTML文件里 <!DOCTYPE html> <html> <head> <scrip ...

  2. MFC下的DLL编程学习

    1.DLL库与LIB库对比: 静态链接库Lib(Static Link Library),是在编译的链接阶段将库函数嵌入到应用程序的内部.如果系统中运行的多个应用程序都包含所用到的公共库函数,则必然造 ...

  3. MongoDB入门---简介

    最近呢,刚好有一些时间,所以就学习了一下新的数据库类型MongoDB.要想了解这个MongoDB,我们首先需要了解一个概念,那就是nosql(not only sql).一下就是官方的概念: NoSQ ...

  4. xss挑战赛小记 0x01(xsstest)

    0x00 今天在先知社区看到了一个xss挑战赛 结果发现比赛已经结束 服务器也关了 百度找了个xss挑战赛来玩一下 正好印证下xss的学习--- 地址     http://test.xss.tv/ ...

  5. 如何在Moodle中显示PPT课件

    Moodle中目前是不直接支持PPT的,所以需要曲线救国: 1.安装 iSpring Free 8版本,免费版,其实是一个PPT的插件,在PPT的工具栏中有显示. 2.打开PPT后,直接在该工具中进行 ...

  6. struts2官方 中文教程 系列六:表单验证

    先贴个本帖的地址,以免被爬:struts2教程 官方系列六:表单验证  即 http://www.cnblogs.com/linghaoxinpian/p/6906720.html 下载本章节代码 介 ...

  7. 第5模块闯关Bootstrap

    “行(row)”必须包含在 .container (固定宽度)或 .container-fluid (100% 宽度)中,以便为其赋予合适的排列(aligment)和内补(padding). 通过“行 ...

  8. spring boot 连接mysql 错误The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one

    1.spring boot 整合mybatis 连接mysql时错误 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or repr ...

  9. PL/SQL查看表结构

    SET LONG 99999;SET LINESIZE 140 PAGESIZE 1000;SELECT DBMS_METADATA.GET_DDL('&OBJECT_TYPE','& ...

  10. HTML5 本地存储Web Storage简单了解

    ​HTML5本地存储规范,定义了两个重要的API :Web Storage  和  本地数据库Web SQL Database. 本地存储Web Storage 实际上是HTML4的cookie存储机 ...