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 ...
随机推荐
- zabbix 插件使用问题
[elk@dr-mysql01 frontend]$ ../../bin/logstash -f std02.conf Settings: Default pipeline workers: 8 Pi ...
- Java如何让异常处理机制更完备规范
1)catch的Exception一定要详细的点名是某种异常而非一概而论的用Exception ex来接收所有的异常,往往不理解这点的人也不能很好的理解catch的意义到底在哪里,是对捕获的异常进行一 ...
- CDH-5.4.3离线安装
使用CM离线安装CDH-5.4.3,如下: cdh5.4.3安装 配置/etc/hosts vim /etc/hosts 192.168.10.1 s1 192.168.10.2 s2 192.168 ...
- hadoop2.2.0 MapReduce的序列化
package com.my.hadoop.mapreduce.dataformat; import java.io.DataInput;import java.io.DataOutput;impor ...
- windows下定时利用bat脚本实现ftp上传和下载
前言: 工作中可能会遇到以下情况,利用windows作为中转,来实现两台linux服务器的文件传输. 实现步骤: 1.FTP上传和下载的bat脚本. 脚本分为两部分:可执行bat脚本和ftp命令文件: ...
- cocos2dx 利用CCOrbitCamera实现扑克牌翻牌效果
[cpp] view plaincopy #include "HelloWorldScene.h" #include "SimpleAudioEngine.h" ...
- JavaScript经典魔力代码
是什么使得JavaScript不同于其他程序设计语言,在浏览器修饰方面表现出其优异的特性?毫无疑问,JavaScript在Web应用领域受到的好评,既源于它自身灵活的动态特性,也源于浏览器对它充分的支 ...
- verilog流水线加法器
四位加法器 两级加法实现 verilog code module pipeliningadder( output reg [3:0] s, output reg co, input [3:0] a, ...
- Resizable(调整大小)组件
一.加载方式 //class 加载方式 <div id="rr" class="easyui-resizable" data-options=" ...
- 《第一行代码》学习笔记37-服务Service(4)
一个比较完整的自定义AsyncTask写成如下: class DownloadTask extends AsyncTask<Void, Integer, Boolean> { @Overr ...