SPOJ:Ada and Graft (set合并&优化)
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合并&优化)的更多相关文章
- 【题解】ADAGRAFT - Ada and Graft [SP33331]
[题解]ADAGRAFT - Ada and Graft [SP33331] 传送门:\(\text{Ada and Graft}\) \(\text{[SP33331]}\) [题目描述] 给出一颗 ...
- 8.2.1.4 Index Merge Optimization 索引合并优化:
8.2.1.4 Index Merge Optimization 索引合并优化: 索引合并方法是用于检索记录 使用多个 范围扫描和合并它们的结果集到一起 mysql> show index fr ...
- requerjs 合并 优化配置
/* * This is an example build file that demonstrates how to use the build system for * require.js. * ...
- LOJ #2537. 「PKUWC 2018」Minimax (线段树合并 优化dp)
题意 小 \(C\) 有一棵 \(n\) 个结点的有根树,根是 \(1\) 号结点,且每个结点最多有两个子结点. 定义结点 \(x\) 的权值为: 1.若 \(x\) 没有子结点,那么它的权值会在输入 ...
- 线段树区间合并优化dp——cf1197E(好)
线段树优化dp的常见套路题,就是先按某个参数排序,然后按这个下标建立线段树,再去优化dp 本题由于要维护两个数据:最小值和对应的方案数,所以用线段树区间合并 /* dp[i]表示第i个套娃作为最内层的 ...
- SPOJ 15. The Shortest Path 堆优化Dijsktra
You are given a list of cities. Each direct connection between two cities has its transportation cos ...
- [BZOJ3681]Arietta(可持久化线段树合并优化建图+网络流)
暴力建图显然就是S->i连1,i->j'连inf(i为第j个力度能弹出的音符),j'->T连T[j]. 由于是“某棵子树中权值在某区间内的所有点”都向某个力度连边,于是线段树优化建图 ...
- 【51nod1674】区间的价值 V2(算法效率--位运算合并优化+链表实现)
题目链接: 51nod1674 题意:规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积.现在l有一个 N 个数的序列,问所有n*(n+1)/2个区间的贡献的和对1 ...
- 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 ...
随机推荐
- mongDB的常用操作总结
目录 常用查询: 查询一条数据 查询子元素集合:image.id gte: 大于等于,lte小于等于... 查询字段不存在的数据not 查询数量: 常用更新 更新第一条数据的一个字段: 更新一条数据的 ...
- NOIP临考经验(转)
[COGS]NOIP临考经验 1. 提前15分钟入场,此时静坐调整心态,适当的深呼吸 2. 打开编辑器并调整为自己喜欢的界面 3. 熟悉文件目录,写好准确无误的代码模板 4. 压缩包或许还不能 ...
- All you need to know about SYN floods
http://blog.dubbelboer.com/ Date: 09 Apr 2012Author: Erik Dubbelboer SYN cookies So one day I notice ...
- BUPT复试专题—寻找i*j=m的个数(2016)
题目描述 3*3的矩阵内容. 1 2 3 2 4 6 3 6 9 即a[i][j](1<=i<=n,1<=j<=n)=i*j. 问一个这样n*n的矩阵里面,里面m出现的次数. ...
- 全志a13开发总结
这几天因为工作的原因,開始接触全志a13芯片,本人在网上搜集了好长时间,可是网上的资料对这方面的描写叙述是很少的, 所以,仅仅能靠数据手冊还有官网上面的英文文档进行开发了,下面仅仅是开发中的非常少的一 ...
- ZOJ ACM 1314(JAVA)
昨天做了几个题目.过于简单,就不在博客里面写了. 1314这道题也比較简单,写出来是由于我认为在这里有一个小技巧,对于时间复杂度和空间复杂度都比較节省. 这个题目类似哈希表的求解.可是更简单.刚拿到题 ...
- PHP将当前目录列出来
$d=dir("."); echo $d->path; while(false !== ($e = $d->read())) { echo "<a hr ...
- WPF MVVM UI分离之《交互与数据分离》 基础才是重中之重~delegate里的Invoke和BeginInvoke 将不确定变为确定系列~目录(“机器最能证明一切”) 爱上MVC3系列~全局异常处理与异常日志 基础才是重中之重~lock和monitor的区别 将不确定变成确定~我想监视我的对象,如果是某个值,就叫另一些方法自动运行 将不确定变成确定~LINQ DBML模型可以对
WPF MVVM UI分离之<交互与数据分离> 在我们使用WPF过程中,不可避免并且超级喜欢使用MVVM框架. 那么,使用MVVM的出发点是视觉与业务逻辑分离,即UI与数据分离 诸如下 ...
- 基于SpringMVC框架使用ECharts3.0实现折线图,柱状图,饼状图,的绘制(上篇)
页面部分 <%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE html> ...
- CSDN-markdown编辑器之从线上导入Markdown文件
CSDN-markdown编辑器支持从线上导入Markdown文件的功能,假设你用其他支持Markdown的编辑器在网上写了博客文章或说明档,想公布到CSDN博客中,就能够使用本功能非常方便的完毕 ...