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 ...
随机推荐
- POJ 2125 最小点权覆盖集(输出方案)
题意:给一个图(有自回路,重边),要去掉所有边,规则:对某个点,可以有2种操作:去掉进入该点 的所有边,也可以去掉出该点所有边,(第一种代价为w+,第二种代价为w-).求最小代价去除所有边. 己思:点 ...
- android 服务
1.创建服务 Exported:是否允许除了当前程序之外的其他程序访问这个服务 Enable:是否启用这个服务 点击完成后自动生成 import android.app.Service; import ...
- nyoj_90_整数划分_201403161553
整数划分 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+…+nk, 其中n1≥n2≥…≥nk≥1,k≥1. 正整数 ...
- SPOJ MIXTURES 区间dp
Harry Potter has n mixtures in front of him, arranged in a row. Each mixture has one of 100 differen ...
- 算法笔记字符串处理问题H:编排字符串(2064)
题目描述 请输入字符串,最多输入4 个字符串,要求后输入的字符串排在前面,例如 输入:EricZ 输出:1=EricZ 输入:David 输出:1=David 2=EricZ 输入:Peter 输出: ...
- android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)
http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...
- 【kotlin】基本语法when的使用,类似于java中的switch,但是又青出于蓝而胜于蓝
when(要判断的参数){ 参数值为1 ->做这种事情 参数值为2 ->做另一种事情 else -> 类似于switch中的default } 扩展使用:https://www.cn ...
- 最大熵推导LR
http://www.win-vector.com/dfiles/LogisticRegressionMaxEnt.pdf https://www.zhihu.com/question/2409455 ...
- linux操作系统下查看某rpm包是32bit 还是x64bit的命令
[root@hosta ~]# rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" | grep l ...
- php操作xml的方法
xml源文件 <?xml version="1.0 encoding="UTF-8"?> <humans> <zhangying> & ...