给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. 206 Reverse Linked List 反转链表

    反转一个单链表.进阶:链表可以迭代或递归地反转.你能否两个都实现一遍?详见:https://leetcode.com/problems/reverse-linked-list/description/ ...

  2. C++中的数学函数汇总

    math.h 数学函数库,一些数学计算的公式的具体实现是放在math.h里,具体有: 1 三角函数 double sin (double); double cos (double); double t ...

  3. 不支持正在使用的 .Net 组帧模式。有关详细信息,请参阅服务器日志--解决方案

    问题在于 NetTcpBinding 服务端和客户端配置不一致. 至少  客户端和服务端:安全性.是否启用可靠会话以及传输方式必须一致 主要是传输方式导致 "不支持正在使用的 .Net 组帧 ...

  4. HashMap和List遍历方法总结及如何遍历删除元素

    https://blog.csdn.net/demohui/article/details/77748809

  5. input 全选 jquery封装方法

    HTML代码 <table class="table table-striped"> <thead> <tr> <th><in ...

  6. Android组件化开发(注意事项)

    1.Manifest合并 在Android studio编译项目时,无论你使用了几个Module都会把所有Manifest最终合并成一个,需要我们注意的是application标签下这个几个属性引用的 ...

  7. Android学习笔记(十) Activity的生命周期

    一.如何在一个应用程序中定义多个Activity -定义一个类,继承Activity -复写onCreate() setContentView(R.layout.secondLayout):设定该Ac ...

  8. Nexus环境搭建

    安装 1.解压nexus-2.11.01-bundle.zip到F:\Java\nexus(可自定义) 2.进入F:\Java\nexus\nexus-2.11.1-01\bin\jsw进入相应的系统 ...

  9. Vuex的全面用法总结

    1. vuex简介 vuex是专门用来管理vue.js应用程序中状态的一个插件.他的作用是将应用中的所有状态都放在一起,集中式来管理.需要声明的是,这里所说的状态指的是vue组件中data里面的属性. ...

  10. Linux 控制终端转义和控制序列

    DESCRIPTION 描述 Linux控制台实现了VT102和ECMA-48/ISO 6429/ANSI X3.64终端控制的子集, 这个子集很庞大,当然还有Linux自己私有的控制序列,以改变颜色 ...