As you might already know, Ada the Ladybug is a farmer. She grows a big fruit tree (with root in 0). There is a fruit on every node of the tree. Ada is competing in grafting competition and this is her masterpiece. The most valuable tree wins the competition. The value of tree is product of values of each node. The value of a node is the number of distinct fruit kinds in its subtree.

Can you find the value of Ada's tree? Since this number might be pretty big, output it modulo 109+7

Input

The first and line will contain 1 ≤ N ≤ 4*105.

The next line will contain N-1 integers 0 ≤ pi < i, the parent of ith node.

The next line will contain N integers 0 ≤ Fi ≤ 109, the fruit growing on ith node.

Output

Print a single integer - the value of tree modulo 1000000007.

Example Input

5
0 0 1 1
1 1 1 2 2

Example Output

4

Example Input

4
0 1 2
6 7 2 3

Example Output

24

Example Input

11
0 1 1 1 3 5 2 7 5 4
494052753 959648710 959648710 959648710 494052753 959648710 959648710 959648710 959648710 494052753 959648710

Example Output

32

题意:给定一棵树,每个节点有自己的颜色,现在求每个节点的子树的颜色种类之积,结果模1e9+7;

思路:用set合并,合并的时候可以用小的集合加到大集合里,得到每个节点的子树有哪些颜色,swap之前保证记录答案就行。(bitset我试过,会超时)。

#include<set>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int Mod=1e9+;
const int maxn=;
set<int>s[maxn];
int a[maxn],id[maxn],f[maxn];
int Laxt[maxn],Next[maxn],To[maxn],cnt;
void read(int &x){
x=;char c=getchar();
while(c>''||c<'') c=getchar();
while(c>=''&&c<=''){
x=(x<<)+(x<<)+c-''; c=getchar();
}
}
void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void merge(int &u,int &v){
if(s[u].size()>s[v].size()) swap(u,v);
set<int>:: iterator it ;
for(it=s[u].begin();it!=s[u].end();it++)
s[v].insert(*it);
}
void dfs(int u,int fa){
for(int i=Laxt[u];i;i=Next[i]){
dfs(To[i],u);
merge(id[To[i]],id[u]);
}
f[u]=s[id[u]].size();
}
int main()
{
int N,ans=,x,i;
scanf("%d",&N);
for(i=;i<N;i++){
read(x); add(x,i);
}
for(i=;i<N;i++){
id[i]=i; read(x);
s[i].insert(x);
}
dfs(,-);
for(i=;i<N;i++) ans=((ll)ans*f[i]%Mod)%Mod;
cout<<ans<<endl;
return ;
}

SPOJ:Ada and Graft (set合并&优化)的更多相关文章

  1. 【题解】ADAGRAFT - Ada and Graft [SP33331]

    [题解]ADAGRAFT - Ada and Graft [SP33331] 传送门:\(\text{Ada and Graft}\) \(\text{[SP33331]}\) [题目描述] 给出一颗 ...

  2. 8.2.1.4 Index Merge Optimization 索引合并优化:

    8.2.1.4 Index Merge Optimization 索引合并优化: 索引合并方法是用于检索记录 使用多个 范围扫描和合并它们的结果集到一起 mysql> show index fr ...

  3. requerjs 合并 优化配置

    /* * This is an example build file that demonstrates how to use the build system for * require.js. * ...

  4. LOJ #2537. 「PKUWC 2018」Minimax (线段树合并 优化dp)

    题意 小 \(C\) 有一棵 \(n\) 个结点的有根树,根是 \(1\) 号结点,且每个结点最多有两个子结点. 定义结点 \(x\) 的权值为: 1.若 \(x\) 没有子结点,那么它的权值会在输入 ...

  5. 线段树区间合并优化dp——cf1197E(好)

    线段树优化dp的常见套路题,就是先按某个参数排序,然后按这个下标建立线段树,再去优化dp 本题由于要维护两个数据:最小值和对应的方案数,所以用线段树区间合并 /* dp[i]表示第i个套娃作为最内层的 ...

  6. SPOJ 15. The Shortest Path 堆优化Dijsktra

    You are given a list of cities. Each direct connection between two cities has its transportation cos ...

  7. [BZOJ3681]Arietta(可持久化线段树合并优化建图+网络流)

    暴力建图显然就是S->i连1,i->j'连inf(i为第j个力度能弹出的音符),j'->T连T[j]. 由于是“某棵子树中权值在某区间内的所有点”都向某个力度连边,于是线段树优化建图 ...

  8. 【51nod1674】区间的价值 V2(算法效率--位运算合并优化+链表实现)

    题目链接:  51nod1674 题意:规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积.现在l有一个 N 个数的序列,问所有n*(n+1)/2个区间的贡献的和对1 ...

  9. SPOJ:Dandiya Night and Violence(Bitset优化)

    It is Dandiya Night! A certain way how dandiya is played is described: There are N pairs of people p ...

随机推荐

  1. Delphi+MySQL:TADOQuery使用插入中文乱码解决方法

    Delphi+MySQL:TADOQuery使用插入中文乱码解决方法 with adoquery dobeginclose;sql.clear;sql.text:=' insert into test ...

  2. java连oracle

    下载连接驱动 安装完oracle之后 D:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib 目录下拷贝 支持jdk1.6以上 From.java ...

  3. CODEVS_1227 方格取数2 网络流 最小费用流 拆点

    原题链接:http://codevs.cn/problem/1227/ 题目描述 Description 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1, ...

  4. 【powerdesign】从mysql数据库导出到powerdesign,生成数据字典

    使用版本powerdesign16.5,mysql 5.5,windows 64 =========================================================== ...

  5. 统计显著性(Statistical significance)

    显著性,又称统计显著性(Statistical significance), 是指零假设为真的情况下拒绝零假设所要承担的风险水平,又叫概率水平,或者显著水平. [1] 显著性的含义是指两个群体的态度之 ...

  6. BeagleBone Black Industrial系统更新设置一贴通

    前言 原创文章,转载引用务必注明链接.水平有限,欢迎指正. 本文使用markdown写成,为获得更好的阅读体验,推荐访问我的博客原文: http://www.omoikane.cn/2016/09/1 ...

  7. 推断dxf文件的版本号

    打开DXF參考手冊,在DXF參考手冊中,点击"索引"-->输入"HEADER",在ACADVER字段有acd的版本号信息: 以下是用C语言,写的推断dxf ...

  8. HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)

    Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...

  9. VB.NE总结

    VB.NET视频看完了.但台湾微软资深讲师的声音还回荡在我的脑海中啊.刚開始听两位老师的讲课那是一个纠 结.感觉不亚于听英语听力训练.后来看到王鹏同学转载的台湾计算机术语和大陆计算机术语的对比,我才明 ...

  10. eclipse创建maven web app

    1 这个功能是由eclipse的插件maven archetype plugin支持的 2 创建过程 File->New->Maven Project 选择archetype为maven- ...