Acyclic Organic Compounds
题意:
给一以1为根的字符树,给出每个节点的字符与权值,记 $diff_{x}$ 为从 $x$ 出发向下走,能走到多少不同的字符串,求问最大的
$diff_{x} + c_{x}$,并求有多少个 $diff_{x} + c_{x}$。
解法:
考虑$dfs$,从下到上启发式合并 $Trie$ 树,效率 $O(nlogn)$。
#include <iostream>
#include <cstdio>
#include <cstring> #define N 300010 using namespace std; struct edge
{
int x,to;
}E[N<<]; struct node
{
node *ch[];
int siz; node* init()
{
siz=;
memset(ch,,sizeof(ch));
return this;
};
}spT[N<<],*root[N]; int n,m,totn,totE,ans,ansv;
int fa[N],g[N],c[N];
char S[N]; void addedge(int x,int y)
{
E[++totE] = (edge){y,g[x]}; g[x]=totE;
E[++totE] = (edge){x,g[y]}; g[y]=totE;
} #define p E[i].x node* merge(node *p1,node *p2)
{
if(p1->siz < p2->siz) swap(p1,p2);
for(int t=;t<;t++)
if(p2->ch[t])
{
if(!p1->ch[t]) p1->ch[t]=p2->ch[t];
else p1->ch[t] = merge(p1->ch[t], p2->ch[t]);
}
p1->siz=;
for(int t=;t<;t++)
if(p1->ch[t]) p1->siz+=p1->ch[t]->siz;
return p1;
} void dfs(int x)
{
int tmp=S[x]-'a';
root[x]=spT[++totn].init();
root[x]->ch[tmp]=spT[++totn].init();
for(int i=g[x];i;i=E[i].to)
if(p!=fa[x])
{
fa[p]=x;
dfs(p);
}
for(int i=g[x];i;i=E[i].to)
if(p!=fa[x])
root[x]->ch[tmp] = merge(root[x]->ch[tmp],root[p]);
root[x]->siz = root[x]->ch[tmp]->siz;
if(root[x]->siz+c[x] > ansv)
{
ansv = root[x]->siz+c[x];
ans=;
}
else if(root[x]->siz+c[x] == ansv) ans++;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) g[i]=;
totE=;
for(int i=;i<=n;i++) scanf("%d",&c[i]);
S[]='*';
scanf("%s",S+);
ans=;
totn=ansv=;
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
}
fa[]=;
dfs();
cout << ansv << endl << ans << endl;
}
return ;
}
Acyclic Organic Compounds的更多相关文章
- Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并
D. Acyclic Organic Compounds You are given a tree T with n vertices (numbered 1 through n) and a l ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)
trie合并的裸题...因为最多只有n个点,所以最多合并n次,复杂度$O(N*26)$. #include<iostream> #include<cstring> #inclu ...
- CF601D:Acyclic Organic Compounds
给n<=300000的树,每个点上有一个字母,一个点的权值为:从该点出发向下走到任意节点停下形成的不同字符串的数量,问最大权值. 题目本身还有一些奇怪要求在此忽略.. Trie合并的模板题. # ...
- cf Round 601
A.The Two Routes(BFS) 给出n个城镇,有m条铁路,铁路的补图是公路,汽车和火车同时从1出发,通过每条路的时间为1,不能同时到达除了1和n的其它点,问他们到达n点最少要用多长时间. ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- Coupled model
常见的coupled models phase English paper WRF-Chem mechanism public data 一些重要的结论 干空气的状态方程 ECWMF驱动WRF 常见的 ...
- skipping the actual organic impact moderation supplied
The most recent running footwear design has gone out. The high cost is actually $150. Expert sports ...
- algorithm@ Shortest Path in Directed Acyclic Graph (O(|V|+|E|) time)
Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths fr ...
随机推荐
- vs2012 MinGW编译ffmpeg 出现libavdevice/avdevice.c(38) : error C2059: 语法错误:“.”
利用vs2012编译ffmpeg出现以下错误: libavdevice/avdevice.c(38) : error C2059: 语法错误:“.” libavdevice/avdevice.c(40 ...
- ARM和X86
嵌入式简介汇总 脚本语言 编程语言 Java C# C ++ 汇编 机器语言 语言 Unix Linux Android + 塞班 + Windows + + + ios系统 基于unix内核的图形化 ...
- 轻松搞定RabbitMQ(五)——路由选择
转自 http://blog.csdn.net/xiaoxian8023/article/details/48733249 翻译地址:http://www.rabbitmq.com/tutorials ...
- iOS 获取图片某一点的颜色对象(UIColor*)。
- (UIColor *)colorAtPixel:(CGPoint)point { // Cancel if point is outside image coordinates if (!CGRe ...
- React机制浅析
在写React代码时,避免不了提前想想页面的结构,当然这也属于HTML布局了,比如哪些组件里需要包含哪些组件.遂突发奇想,如果试着把子组件的render内容替换原组件,会是个啥? 比如拿 https: ...
- JavaScript读书笔记(4)-变量、作用域和内存问题
1.ECMAScript数据类型分为:基本类型值和引用类型值: ECMAScript中所有函数的参数都是按值传递的: 检查对象的类型:varible instanceof constructor Al ...
- 怎样高速编译mediatek\operator以下代码
mediatek\operator以下有单独的apk.也有overlay的数据,单独的apk会配置anroid.mk,找到相应的路径直接build. 假设是overlay,则编译原来应用的路径,比如 ...
- sublime 快捷键 汇总--长期
Ctrl+P 输入当前项目中的文件名,快速搜索文件 Ctrl+G 输入数字跳转到该行代码 Ctrl+R 输入关键字,查找文件中的函数名 Ctrl+: 输入关键字,查找文件中的变量名.属性名等 Ctrl ...
- 寻找JAVA_HOME find java
more apache-flume-1.8.0-bin/bin/flume-ng # find java if [ -z "${JAVA_HOME}" ] ; then warn ...
- [通信]Linux User层和Kernel层常用的通信方式
转自:https://bbs.csdn.net/topics/390991551?page=1 netlink:https://blog.csdn.net/stone8761/article/deta ...