Codeforces 543D. Road Improvement (树dp + 乘法逆元)
题目链接: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 + 乘法逆元)的更多相关文章
- Codeforces 543D Road Improvement(DP)
题目链接 Solution 比较明显的树形DP模型. 首先可以先用一次DFS求出以1为根时,sum[i](以i为子树的根时,满足要求的子树的个数). 考虑将根从i变换到它的儿子j时,sum[i]产生的 ...
- Codeforces 543D Road Improvement(树形DP + 乘法逆元)
题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...
- Codeforces 543D Road Improvement
http://codeforces.com/contest/543/problem/D 题意: 给定n个点的树 问: 一开始全是黑边,对于以i为根时,把树边白染色,使得任意点走到根的路径上不超过一条黑 ...
- BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...
- Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp
D - Road Improvemen 思路:0没有逆元!!!! 不能直接除,要求前缀积和后缀积!!! #include<bits/stdc++.h> #define LL long lo ...
- Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)
题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...
- Codeforces 1332F - Independent Set(树dp)
题目链接 题意 给出一棵 n 个点的树, 求它的所有非空诱导子图的独立集种类数之和, 对 998244353 取模. n ≤ 3e5. 题解 不妨假设在独立集中的点被染色成 1, 其余不染色; 由于不 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- C++实现顺序表
#include<iostream>using namespace std; typedef int DataType; class SeqList{public: SeqList( ...
- Android telnet RPi 2B
/************************************************************************* * Android telnet RPi 2B * ...
- 解决android sdk manage打开闪退的解决方法
在打开android sdk mangage.exe的时候,一闪而过,在eclipse中出现如下提示: [2015-07-20 13:42:23 - SDK Manager] [SDK Manager ...
- exception is org.hibernate.exception.DataException: Could not execute JDBC batch update at
没有什么问题,但是却报了Could not execute JDBC batch update的错,主要是配置文件设置了关联,数据却没有关联造成的,只要数据正确就没有问题. 另外,造成这个原因的还可能 ...
- 【英语】Bingo口语笔记(26) - Take系列
raincheck 改日 take .. off 在这里是请假的意思
- Defining Database and Instance【数据库与实例】
Database: A collection of physical operating system files or disks. When usingOracle Automatic Stora ...
- nodejs的调试(node-inspector)
我们在接触客户端javascript的时候,调试利器就是firebug ,也是当年为何喜欢用上firefox 浏览器的主要动力,当然,后来 chrome 插件里也出现了firebug的身影..... ...
- Fidder 监控WCF
Client端配置 <?xml version="1.0" encoding="utf-8" ?> <configuration> &l ...
- 嵌入式 H264视频通过RTMP直播
前面的文章中提到了通过RTSP(Real Time Streaming Protocol)的方式来实现视频的直播,但RTSP方式的一个弊端是如果需要支持客户端通过网页来访问,就需要在在页面中嵌入一个A ...
- 嵌入式 hi3518x平台h264+g711a封装mp4代码demo
先看代码吧,有代码有真相,具体代码的demo(下载demo的朋友请勿在网上上传我的demo,谢谢)下载连接为: http://download.csdn.net/detail/skdkjxy/8071 ...