CF601D:Acyclic Organic Compounds
给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的更多相关文章
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- 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 ...
- Acyclic Organic Compounds
题意: 给一以1为根的字符树,给出每个节点的字符与权值,记 $diff_{x}$ 为从 $x$ 出发向下走,能走到多少不同的字符串,求问最大的$diff_{x} + c_{x}$,并求有多少个 $di ...
- Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)
trie合并的裸题...因为最多只有n个点,所以最多合并n次,复杂度$O(N*26)$. #include<iostream> #include<cstring> #inclu ...
- 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的数取 ...
- Linux新手必看:浅谈如何学习linux
本文在Creative Commons许可证下发布 一.起步 首先,应该为自己创造一个学习linux的环境--在电脑上装一个linux或unix问题1:版本的选择 北美用redhat,欧洲用SuSE, ...
- [转载] Linux新手必看:浅谈如何学习linux
本文转自 https://www.cnblogs.com/evilqliang/p/6247496.html 本文在Creative Commons许可证下发布 一.起步 首先,应该为自己创造一个学习 ...
- Coupled model
常见的coupled models phase English paper WRF-Chem mechanism public data 一些重要的结论 干空气的状态方程 ECWMF驱动WRF 常见的 ...
随机推荐
- Android开发学习——开发调试工具-DDMS应用,ADB进程,Logcat,Eclipse Debug调试
DDMS -- Dalvik debug monitor service 调试监控服务 对模拟器进行相关配置. ADB进程 Android debug bridge 建立eclipse和 ...
- Windows10系统切换JDK版本(前提是装了多个版本的JDK)
由于是直接截屏,等我回过头来整理的时候忘记了文章原来的出处, 如作者本人看到,如有侵权,请联系删除!
- 【Java】包装类型
Java中的基本类型功能简单,不具备对象的特性,为了使基本类型具备对象的特性,所以出现了包装类,就可以像操作对象一样操作基本类型数据. 一.基本类型对应的包装类 基本类型 ...
- hihocoder offer收割编程练习赛11 D 排队接水
思路: 莫队算法+树状数组. 莫队算法的基本思想是对大量要查询的区间进行离线处理,按照一定的顺序计算,来降低复杂度.概括来说,我们在知道了[l, r]的解,并且可以通过一个较低的复杂度推出[l - 1 ...
- css中border制作各种形状
css利用border制作各种形状的原理如图: 使用border绘制三角形是什么原理?事实上,宽度相等的border是以45度对接的,如下图: 没有了上border如图所示: 再设置border的宽度 ...
- 重构28-Rename boolean method(重命名布尔方法)
你也可以说这并不是一个真正的重构,因为方法实际上改变了,但这是一个灰色地带,可以开放讨论.一个拥有大量布尔类型参数的方法将很快变得无法控制,产生难以预期的行为.参数的数量将决定分解的方法的数量.来看看 ...
- iOS----轻松掌握AFN网络顶级框架
AFN 一.什么是AFN 全称是AFNetworking,是对NSURLConnection的一层封装 虽然运行效率没有ASI高,但是使用比ASI简单 在iOS开发中,使用比较广泛 AFN的githu ...
- python3爬取微博评论并存为xlsx
python3爬取微博评论并存为xlsx**由于微博电脑端的网页版页面比较复杂,我们可以访问手机端的微博网站,网址为:https://m.weibo.cn/一.访问微博网站,找到热门推荐链接我们打开微 ...
- OracleService類
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- springboot Tomcat connector configured to listen on port 8081 failed to start.
启动报 Tomcat connector configured to listen on port 8081 failed to start. The port may already be in ...