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. (2)Swing窗体基本设置

    import javax.swing.*; import javax.swing.plaf.FontUIResource; import java.awt.*; import java.util.En ...

  2. 分布式架构和微服务CI/CD的范本技术解读

    随笔分类 - 分布式架构--http://www.cnblogs.com/hujihon/category/858846.html (ZooKeeper.activemq.redis.kafka)的分 ...

  3. Hijacking FM Radio with a Raspberry Pi & Wire

    转载:https://null-byte.wonderhowto.com/how-to/hack-radio-frequencies-hijacking-fm-radio-with-raspberry ...

  4. Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf

    Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensi ...

  5. [Tools] Region commands to collapse the code by group

    For a file which contians lots of lines of code, we can use 'comments region' to collapse the code. ...

  6. Lambda 表达式的演示样例-来源(MSDN)

    本文演示怎样在你的程序中使用 lambda 表达式. 有关 lambda 表达式的概述.请參阅 C++ 中的 Lambda 表达式. 有关 lambda 表达式结构的具体信息,请參阅 Lambda 表 ...

  7. Log4cpp 使用手册

    参考资料: log4cpp 配置 与 使用http://www.cnblogs.com/welkinwalker/archive/2011/06/23/2088197.html 便利的开发工具-log ...

  8. HTML5面试题-b

    感谢分享 面试有几点需要注意: 面试题目: 根据你的等级和职位变化,入门级到专家级:范围↑.深度↑.方向↑. 题目类型: 技术视野.项目细节.理论知识型题,算法题,开放性题,案例题. 进行追问: 可以 ...

  9. Android Auto Scroll ViewPager (Smooth)

    项目信息 项目地址 https://github.com/benniaobuguai/android-auto-scroll-viewpager 特性 支持自动平滑切换海报.相册等,无限次循环轮播 手 ...

  10. 记录:50多行程序中找出多写的一个字母e

    小霍同学调程序,做的是第11周的项目1 - 存储班长信息的学生类,可是她写的程序(就在以下),呃,请读者自己执行一下吧.(下午在机房调试时用的是Code::Blocks10.05.输出的是非常长的莫名 ...