繁华模拟赛 Evensgn剪树枝


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int maxn = ;
const ll mod = ;
struct edge{
int to;
int nxt;
};
int n,f[maxn][],col[maxn];
int cnt,head[maxn];
edge e[maxn];
void add_edge(int u,int v){
cnt++;
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
ll q_mul(ll a,ll b){
ll ans = ;
while(b){
if(b&){
ans = (ans + a) % mod;
}
a = (a + a) % mod;
b >>= ;
}
return ans;
}
ll q_pow(ll a,ll b){
ll ans = ;
while(b){
if(b&){
ans = q_mul(ans,a);
}
a = q_mul(a,a);
b >>= ;
}
return ans;
}
void input(){
cin>>n;
int cmd;
for(int i = ;i < n;i++){
cin>>cmd;
add_edge(cmd,i);
}
for(int i = ;i < n;i++){
cin>>col[i];
}
}
void dfs(int u){
if(!head[u]){
if(col[u]) f[u][] = ;
else f[u][] = ;
return;
}
ll sum = ;
int v;
if(col[u]){
for(int i = head[u];i;i = e[i].nxt){
v = e[i].to;
dfs(v);
sum = q_mul(sum,f[v][] + f[v][]);
}
f[u][] = sum;
return;
}else{
for(int i = head[u];i;i = e[i].nxt){
v = e[i].to;
dfs(v);
sum = q_mul(sum,f[v][] + f[v][]);
}
f[u][] = sum;
for(int i = head[u];i;i = e[i].nxt){
v = e[i].to;
f[u][] += q_mul(f[v][],q_mul(sum,q_pow(f[v][] + f[v][],mod-)));
}
return;
}
}
int main(){
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
ios::sync_with_stdio(false);
input();
dfs();
cout<<f[][];
return ;
} #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std; #define ll long long
const int MAXN = 1e5 + ;
const int MOD = 1e9 + ; ll f[MAXN][];
int point[MAXN] = {}, nxt[MAXN * ] = {}, v[MAXN * ] = {}, tot = ;
bool color[MAXN] = {};
int n; inline void addedge(int x, int y)
{
tot++;
nxt[tot] = point[x]; point[x] = tot; v[tot] = y;
} void dfs(int now, int father)
{
f[now][] = ;
f[now][] = ;
for (int tmp = point[now]; tmp; tmp = nxt[tmp])
if (v[tmp] != father)
{
dfs(v[tmp], now);
f[now][] = (f[now][] * f[v[tmp]][]) % MOD;
f[now][] = (f[now][] + f[now][] * f[v[tmp]][]) % MOD;
f[now][] = (f[now][] * f[v[tmp]][]) % MOD;
}
if (color[now])
f[now][] = f[now][];
else
f[now][] = (f[now][] + f[now][]) % MOD;
} int main()
{
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
int x;
scanf("%d", &x);
addedge(i, x + ); addedge(x + , i);
}
for (int i = ; i <= n; i++)
{
int x;
scanf("%d", &x);
if (x == ) color[i] = true;
else color[i] = false;
}
dfs(, );
cout << f[][] << endl;
}
繁华模拟赛 Evensgn剪树枝的更多相关文章
- [繁华模拟赛]Evensgn 剪树枝
Evensgn 剪树枝 题目 繁华中学有一棵苹果树.苹果树有 n 个节点(也就是苹果),n − 1 条边(也就 是树枝).调皮的 Evensgn 爬到苹果树上.他发现这棵苹果树上的苹果有两种:一 种是 ...
- 繁华模拟赛 Evensgn与字符矩阵
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 繁华模拟赛 Evensgn玩序列
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 繁华模拟赛 Evensgn的债务
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 【NOIP 模拟赛】Evensgn 剪树枝 树形dp
由于树规做的少所以即使我考试想出来正确的状态也不会转移. 一般dp的转移不那么繁杂(除了插头.....),即使多那也是清晰明了的,而且按照树规的一般思路,我们是从下到上的,所以我们要尽量简洁地从儿子那 ...
- 繁华模拟赛day8 牛栏
/* 标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小 ...
- 繁华模拟赛day8 字典序
/* 这个题要我们求一个字典序,字符串给出的顺序,会对字母的字典序前后相对顺序进行限定,如何用来表示这种限定,我们注意到这种一个之后接着一个,只有先输出他前面的才能输出他,很明显就是拓扑排序,最小方案 ...
- 繁华模拟赛day8 科技树
/* 贪心,很明显是越容易升级的越先升级 */ #include<iostream> #include<cstdio> #include<string> #incl ...
- 繁华模拟赛 Vicent坐电梯
/*n<=5000这样就不能用O(n)的转移了,而是要用O(1)的转移.注意我们每次的转移都来自一个连续的区间,而且我们是求和区间求和?前缀和!令sum[step][i]表示f[ste ...
随机推荐
- .NET Core手记 - Json.NET的使用
Json.NET大家很熟悉了,很流行的高性能Json库,很棒的是Json.NET也支持了.NET Standard框架,也就意味着我们可以在.NET Core项目里使用了. 创建一个.NET Core ...
- 浅析C#中的“==”和Equals
1.“==”和Equals两个真的有关联吗? 对于“==”和Equals大多数网友都是这样总结的: “==” 是比较两个变量的值相等. Equals是比较两个变量是否指向同一个对象. 如:这篇文章,并 ...
- 云计算之路-阿里云上:Wireshark抓包分析一个耗时20秒的请求
这篇博文分享的是我们针对一个耗时20秒的请求,用Wireshark进行抓包分析的过程. 请求的流程是这样的:客户端浏览器 -> SLB(负载均衡) -> ECS(云服务器) -> S ...
- CreateCompatibleDC与BitBlt 学习
CreateCompatibleDC与BitBlt CreateCompatibleDC 创建一个与指定设备一致的内存设备描述表. HDC CreateCompatibleDC(HDC hdc //设 ...
- Oracle添加数据文件创建表空间,创建用户代码
1,添加数据文件创建表空间 CREATE TABLESPACE "TEST1" DATAFILE 'D:\ORACLE\11G\ORADATA\ORCL\TEST1.DBF' SI ...
- JS实现Ajax---例:获取服务器时间
Ajax在本质上是一个浏览器端的技术 XMLHttpRequest XMLHttpRequest对象 XMLHttpRequest对象在IE浏览器和非IE浏览器中创建的方法不同. 简而言之:它可以异步 ...
- Java基础-String 存储机制管理
JVM运行的时候,将内存分为两个部分,一部分是堆,一部分是栈.堆中存放的是创建对象,而栈中存放的则是方法调用过程中的局部变量或引用.在设计JAVA字符串对象内存实现的时候,在堆中又开辟了一块很小的内存 ...
- 【CodeForces 618C】Constellation
题 Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars n ...
- Oracle导出导入数据库的方式
一.导入导出.dmp文件 利用cmd的操作命令导出,详情如下(备注:方法二是转载网上的教程):1:G:\Oracle\product\10.1.0\Client_1\NETWORK\ADMIN目录下有 ...
- request.getAttribute() 和 request.getParameter() 有何区别?
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别: (1)HttpServletRequest类有setAttri ...