给n<=300000的树,每个点上有一个字母,一个点的权值为:从该点出发向下走到任意节点停下形成的不同字符串的数量,问最大权值。

题目本身还有一些奇怪要求在此忽略。。

Trie合并的模板题。

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
//#include<queue>
#include<algorithm>
#include<iostream>
using namespace std; bool isdigit(char c) {return c>='' && c<='';}
int qread()
{
char c;int s=,t=;while (!isdigit(c=getchar())) (c=='-' && (t=-));
do s=s*+c-''; while (isdigit(c=getchar())); return s*t;
} int n;
#define maxn 300011
struct Edge{int to,next;}edge[maxn<<];int first[maxn],le=;
void in(int x,int y) {Edge &e=edge[le];e.to=y;e.next=first[x];first[x]=le++;}
void insert(int x,int y) {in(x,y);in(y,x);} int c[maxn];
char s[maxn];
struct Trie
{
int ch[maxn<<][],size,val[maxn<<];
Trie() {memset(ch[],,sizeof(ch[]));size=;}
int id(char c) {return c-'a';}
void up(int x)
{
val[x]=;
for (int i=;i<;i++)
if (ch[x][i]) val[x]+=val[ch[x][i]];
}
int New(char c)
{
size++;memset(ch[size],,sizeof(ch[size]));
val[size]=;
size++;memset(ch[size],,sizeof(ch[size]));
ch[size-][id(c)]=size;
val[size]=;
return size-;
}
int combine(int x,int y)
{
if (!x || !y) return x+y;
for (int i=;i<;i++)
ch[x][i]=combine(ch[x][i],ch[y][i]);
up(x);
return x;
}
}t; int root[maxn],dif[maxn];
inline void dfs(int x,int fa)
{
root[x]=t.New(s[x]);
int base=;
for (int i=first[x];i;i=edge[i].next)
{
const Edge &e=edge[i]; if (e.to==fa) continue;
dfs(e.to,x);
if (!base) base=e.to;
else root[base]=t.combine(root[base],root[e.to]);
}
if (base)
{
int u=t.ch[root[x]][t.id(s[x])];
for (int i=;i<;i++) t.ch[u][i]=t.ch[root[base]][i];
t.up(u);t.up(root[x]);
}
dif[x]=t.val[root[x]]-;
// cout<<x<<' '<<dif[x]<<endl;
} int main()
{
n=qread();
for (int i=;i<=n;i++) c[i]=qread();
scanf("%s",s+);
for (int i=,x,y;i<n;i++)
{
x=qread(),y=qread();
insert(x,y);
}
dfs(,);
int ans=,cnt=;
for (int i=;i<=n;i++)
{
if (c[i]+dif[i]>ans) ans=c[i]+dif[i],cnt=;
else if (c[i]+dif[i]==ans) cnt++;
}
printf("%d\n%d\n",ans,cnt);
return ;
}

CF601D:Acyclic Organic Compounds的更多相关文章

  1. 【CodeForces】601 D. Acyclic Organic Compounds

    [题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...

  2. 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 ...

  3. Acyclic Organic Compounds

    题意: 给一以1为根的字符树,给出每个节点的字符与权值,记 $diff_{x}$ 为从 $x$ 出发向下走,能走到多少不同的字符串,求问最大的$diff_{x} + c_{x}$,并求有多少个 $di ...

  4. Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)

    trie合并的裸题...因为最多只有n个点,所以最多合并n次,复杂度$O(N*26)$. #include<iostream> #include<cstring> #inclu ...

  5. cf Round 601

    A.The Two Routes(BFS) 给出n个城镇,有m条铁路,铁路的补图是公路,汽车和火车同时从1出发,通过每条路的时间为1,不能同时到达除了1和n的其它点,问他们到达n点最少要用多长时间. ...

  6. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  7. Linux新手必看:浅谈如何学习linux

    本文在Creative Commons许可证下发布 一.起步 首先,应该为自己创造一个学习linux的环境--在电脑上装一个linux或unix问题1:版本的选择 北美用redhat,欧洲用SuSE, ...

  8. [转载] Linux新手必看:浅谈如何学习linux

    本文转自 https://www.cnblogs.com/evilqliang/p/6247496.html 本文在Creative Commons许可证下发布 一.起步 首先,应该为自己创造一个学习 ...

  9. Coupled model

    常见的coupled models phase English paper WRF-Chem mechanism public data 一些重要的结论 干空气的状态方程 ECWMF驱动WRF 常见的 ...

随机推荐

  1. SQLServer 2012 高效分页

    SQLSERVER2012 出新分页功能啦!!!近两天我在自己工作机的PC(没有并发,单一线程)上做了SqlServer  2000/ (2005/2008)/2012三个版本下的分页性能比较. 大致 ...

  2. 关于线程间操作无效: 从不是创建控件“xx”的线程访问它,错误解决方法(自定义委托和系统委托Action或Func解决)

    这是一个线程不安全的问题.跨线程操作问题. 比如我们需要在线程中改变textbox的文本,textbox的name是txtShowMsg 第一种方法(不推荐使用) 在窗体构造函数中写Control.C ...

  3. 工作中js和jquery 函数收集

    1. 判断单选框是否选中  $("xxx").is(":checked")       返回类型是 true/false 2. $(this)[0].nextS ...

  4. AJPFX总结IO流中的缓冲思想

    缓冲思想   (因为内存的运算速度要远大于硬盘的原酸速度,所以只要降低硬盘的读写次数,就可以提高效率)    1. 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,    2. 这是加 ...

  5. AJPFX总结正则表达式的概述和简单使用

    正则表达式的概述和简单使用* A:正则表达式        * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用.        * 作用:比如注 ...

  6. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第二天(非原创)

    文章大纲 一.课程介绍二.整合淘淘商城ssm项目三.Mybatis分页插件PageHelper使用四.整合测试五.项目源码与资料下载六.参考文章   一.课程介绍 一共14天课程(1)第一天:电商行业 ...

  7. 在SQLServer 2005附加SQLServer 2008数据库异常处理

    远程服务器软件系统不算新,数据库是SQL Server 2005.本地开发基本是用新的软件系统.数据库采用SQL Server 2008. 这样在用远程服务器SQL 2005选择附加SQL 2008的 ...

  8. 浅谈2015新版 U-Boot

    过了挺长一断时间没有看U-BOOT了,这两天下载了新版的UBOOT源码(之前看的一些书都是基于早好多年的源码来讲述,总感觉心里有点不对劲,也许是我比较喜新的原因吧,不过小弟我并没有厌旧哈),好了不多扯 ...

  9. (转)淘淘商城系列——zookeeper单机版安装

    http://blog.csdn.net/yerenyuan_pku/article/details/72717744 这篇文章,我单独来说一下zookeeper如何安装.在实际开发中,zookeep ...

  10. ROS在rviz中实时显示轨迹(nav_msgs/Path消息的使用)

    消息结构说明nav_msgs/Path.msg结构#An array of poses that represents a Path for a robot to followHeader heade ...