Codeforces 543D Road Improvement
http://codeforces.com/contest/543/problem/D
题意:
给定n个点的树
问:
一开始全是黑边,对于以i为根时,把树边白染色,使得任意点走到根的路径上不超过一条黑边,输出染色的方案数(mod 1e9+7)
思路:得知f[x]=(f[s1]+1)*(f[s2]+1)*(f[s3]+1)..*(f[sn]+1),s为x的儿子
因为要么这条边修了,里面有f[s1]方案,要吗不修,那剩下的都必须修。
由于要计算每个点的答案。
我们令up[x]为x父亲为x儿子时的f答案。可知up[x]=(fa的f前缀*fa的f后缀*up[fa])+1
最后f[x]*up[x]就是答案。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#define ll long long
const ll Mod=;
int tot,go[],first[],next[],n;
ll up[];
ll f[];
std::vector<int>G[];
std::vector<ll>L[],R[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void dfs1(int x,int fa){
f[x]=;
for (int i=;i<G[x].size();i++){
int pur=G[x][i];
if (pur==fa){
L[x].push_back();
R[x].push_back();
continue;
}
dfs1(pur,x);
f[x]=(f[x]*(f[pur]+))%Mod;
L[x].push_back(f[pur]+);
R[x].push_back(f[pur]+);
}
for (int i=;i<L[x].size();i++)
L[x][i]=(L[x][i]*L[x][i-])%Mod;
for (int i=R[x].size()-;i>=;i--)
R[x][i]=(R[x][i]*R[x][i+])%Mod;
}
void dfs2(int x,int fa,ll val){
up[x]=val;
ll tmp;
for (int i=;i<G[x].size();i++){
int pur=G[x][i];
if (pur==fa) continue;
tmp=val;
if (i>) tmp=(tmp*L[x][i-])%Mod;
if (i<G[x].size()-) tmp=(tmp*R[x][i+])%Mod;
dfs2(pur,x,tmp+);
}
}
void solve(){
dfs1(,);
up[]=;
dfs2(,,1LL);
for (int i=;i<=n;i++)
printf("%I64d ",f[i]*up[i]%Mod);
}
int main(){
n=read();for (int i=;i<=n;i++){int x=read();G[x].push_back(i);G[i].push_back(x);}
solve();
}
Codeforces 543D Road Improvement的更多相关文章
- Codeforces 543D. Road Improvement (树dp + 乘法逆元)
题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...
- Codeforces 543D Road Improvement(树形DP + 乘法逆元)
题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...
- Codeforces 543D Road Improvement(DP)
题目链接 Solution 比较明显的树形DP模型. 首先可以先用一次DFS求出以1为根时,sum[i](以i为子树的根时,满足要求的子树的个数). 考虑将根从i变换到它的儿子j时,sum[i]产生的 ...
- 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 ...
- CodeForces 543D:Road Improvement
题目:http://codeforces.com/problemset/problem/543/D 题意:给你一棵树,一开始边都是0,可以使任意的边变成1,对于每一个根节点求使得它到其他任一点的路径上 ...
- CodeForces 543D 树形DP Road Improvement
题意: 有一颗树,每条边是好边或者是坏边,对于一个节点为x,如果任意一个点到x的路径上的坏边不超过1条,那么这样的方案是合法的,求所有合法的方案数. 对于n个所有可能的x,输出n个答案. 分析: 题解 ...
- Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp
D - Road Improvemen 思路:0没有逆元!!!! 不能直接除,要求前缀积和后缀积!!! #include<bits/stdc++.h> #define LL long lo ...
- Codeforces 702D Road to Post Office(模拟 + 公式推导)
题目链接:http://codeforces.com/problemset/problem/702/D 题意: 一个人要去邮局取东西,从家到达邮局的距离为 d, 它可以选择步行或者开车,车每走 k 公 ...
- Codeforces 240E. Road Repairs 最小树形图+输出路径
最小树形图裸题,只是须要记录路径 E. Road Repairs time limit per test 2 seconds memory limit per test 256 megabytes i ...
随机推荐
- HDOJ 1302(UVa 573) The Snail(蜗牛爬井)
Problem Description A snail is at the bottom of a 6-foot well and wants to climb to the top. The sna ...
- Android中PopupWindow中有输入框时无法弹出输入法的解决办法
PopupWindow window=new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); //必须让p ...
- hdu4324 拓扑排序
#include<cstdio> #include<string.h> #define maxn 2013 char M[maxn][maxn]; int du[maxn]={ ...
- 简单的新闻客户端APP开发(DCloud+thinkphp+scrapy)
前端时间花了1个月左右,搞了个新闻APP,功能很简单,就是把页面版的新闻条目定时爬到后台数据库,然后用app显示出来. 1.客户端 使用了DCloud框架,js基本是个新手,从没写过像样的代码,htm ...
- angularJS怎么实现与服务端的PHP进行数据交互
//{params: 要传的参数obj },params这个是关键字不能换别的变量 $http.get(url, {params: {id: categoryid, key: keys} }).suc ...
- struts2.1.*中再实现了一个servlet的方法
学习Struts2也有一段时间了,今天用Servlet写了一个验证码,然后搬到Struts2中,惊奇地发现Servlet无法访问,出现404错误!后来折腾了半天,终于找出原因了.这也算我学习中的一个重 ...
- [React] React Router: Nested Routes
Since react-router routes are components, creating nested routes is as simple as making one route a ...
- android离线下载的相关知识
离线下载的功能点如下: 1.下载管理(开始.取消下载). 2.网络判断(Wi-Fi,3G). 3.独立进程. 4.定时和手机催醒. 5.自启动. 选择 ...
- How to get multi-touch working(Linux and Andriod)
1.在hid-ids.h中加入vid pid 2.在hid-multitouch..c->mt_devices[] 中加入 { ...
- CVE-2015-8660分析
0x00测试环境 使用环境 备注 操作系统 Ubuntu15.04 虚拟机 内核版本 3.19.0-15-generic 漏洞来源 /fs/overlayfs/inode.c Before 201 ...