题目链接:http://codeforces.com/contest/543/problem/D

给你一棵树,初始所有的边都是坏的,要你修复若干边。指定一个root,所有的点到root最多只有一个坏边。以每个点为root,问分别有多少种方案数。

dp[i]表示以i为子树的root的情况数,不考虑父节点,考虑子节点。   dp[i] = dp[i] * (dp[i->son] + 1)

up[i]表示以i为子树的root的情况数(倒着的),考虑父节点,不考虑子节点。  这里需要逆元。 注意(a/b)%mod中b%mod=0是错误的,所以要特殊判断。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 2e5 + ;
LL dp[N], mod = 1e9 + , up[N];
vector <int> edge[N];
int cnt[N]; //子树(dp[i->son] + 1)%mod != 0的节点数
LL fuck[N]; //子树(dp[i-son] + 1)%mod != 0的方案数相乘 LL fpow(LL a, LL n) {
LL res = ;
while(n) {
if(n & )
res = res * a % mod;
a = a * a % mod;
n >>= ;
}
return res;
} void dfs1(int u, int p) {
dp[u] = ;
fuck[u] = ;
for(int i = ; i < edge[u].size(); ++i) {
int v = edge[u][i];
if(v == p)
continue;
dfs1(v, u);
if(dp[v] + == mod)
cnt[u]++;
else
fuck[u] = ( + dp[v]) % mod * fuck[u] % mod;
dp[u] = ( + dp[v]) % mod * dp[u] % mod;
}
} void dfs2(int u, int p) {
for(int i = ; i < edge[u].size(); ++i) {
int v = edge[u][i];
if(v == p)
continue;
//LL temp = dp[u] * fpow((dp[v] + 1) % mod, mod - 2) % mod; //error
LL temp = ;
if(dp[v] + == mod && up[u] && cnt[u] == ) { //特殊情况
temp = fuck[u];
} else {
temp = dp[u] * fpow((dp[v] + ) % mod, mod - ) % mod;
}
up[v] = (up[u] * temp % mod + ) % mod;
dfs2(v, u);
}
} int main()
{
int n, u;
scanf("%d", &n);
for(int i = ; i <= n; ++i) {
scanf("%d", &u);
edge[i].push_back(u);
edge[u].push_back(i);
}
dfs1(, -);
up[] = ;
dfs2(, -);
for(int i = ; i <= n; ++i) {
printf("%lld%c", dp[i]*up[i]%mod, i == n ? '\n': ' ');
}
return ;
}

Codeforces 543D. Road Improvement (树dp + 乘法逆元)的更多相关文章

  1. Codeforces 543D Road Improvement(DP)

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

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

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

  3. Codeforces 543D Road Improvement

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

  4. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

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

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

  6. Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)

    题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...

  7. Codeforces 1332F - Independent Set(树dp)

    题目链接 题意 给出一棵 n 个点的树, 求它的所有非空诱导子图的独立集种类数之和, 对 998244353 取模. n ≤ 3e5. 题解 不妨假设在独立集中的点被染色成 1, 其余不染色; 由于不 ...

  8. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  9. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

随机推荐

  1. 51nod1086 背包问题 V2

    我都快不会写二进制优化多重背包了...卡了一下常数从rank100+到20+... #include<cstdio> #include<cstring> #include< ...

  2. memcached内存管理及key value长度限制

    1)什么是内存碎片?内存是大小有限的资源.例如把内存比作一张小床,来了一个小伙伴,可以睡下,再来一个小伙伴也能睡下.现在两个人了,他们点了差不多的大小的位置(资源),位置还有剩下.然后再来一个小胖子, ...

  3. 多层感知机及其BP算法(Multi-Layer Perception)

    Deep Learning 近年来在各个领域都取得了 state-of-the-art 的效果,对于原始未加工且单独不可解释的特征尤为有效,传统的方法依赖手工选取特征,而 Neural Network ...

  4. mysql 表日常变化前几

    mysql 表日常变化前几use performance_schema create table test.snap1 as SELECT OBJECT_SCHEMA, OBJECT_NAME, CO ...

  5. 【转】Linux设备驱动之Ioctl控制

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275372.html 大部分驱动除了需要具备读写设备的能力之外,还需要具备对硬件控制的能 ...

  6. js COOKIE 记住帐号或者uuid

    当开始接到这个任务的时候,我对cookie还是没多少了解的,而uuid的生成也是一无所知.但是当你发现这个网址http://stackoverflow.com/questions/105034/how ...

  7. Shell教程2-变量

    Shell支持自定义变量. 定义变量 定义变量时,变量名不加美元符号($),如: 复制纯文本新窗口   variableName="value" 注意,变量名和等号之间不能有空格, ...

  8. ArcEngine9.3报错Create output feature class failed

    ArcEngine9.3执行IFeatureDataConverter.ConvertFeatureClass Method出错如下错误信息: Create output feature class ...

  9. Event/window.Event属性和方法

    type:事件的类型,如onlick中的click:srcElement/target:事件源,就是发生事件的元素:button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下 ...

  10. [偏微分方程教程习题参考解答]4.1Duhamel 原理

    1. 如果已知下述常微分方程的特定初值问题 $$\bex \sedd{\ba{ll} -y''+y=0,&x>0,\\ y(0)=0,\quad y'(0)=1 \ea} \eex$$ ...