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 常见的 ...
随机推荐
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- php中除法取整的方法(round,ceil,floor)
PHP中遇到需要将除法所得结果取整的情况时,就需要用到以下方法: 1. round:四舍五入 round() 函数对浮点数进行四舍五入. 语法:round(x, prec) 参数 描述 x 可选.规定 ...
- LN : leetcode 413 Arithmetic Slices
lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...
- win7+idea+maven搭建spark源码阅读环境
1.参考. 利用IDEA工具编译Spark源码(1.60~2.20) https://blog.csdn.net/He11o_Liu/article/details/78739699 Maven编译打 ...
- 使用Jenkins进行android项目的自动构建(3)
建立Jenkins项目 1. “新增作业”->填写作业名称->选择“建置 Maven 2 或 3 專案”->OK.新增成功后会进入“組態設定”,暂时先保留默认值,稍后再进行设定. 2 ...
- 有意思的String字符工具类
对String的操作是Java攻城师必备的,一个优秀的攻城师是懒惰,他会把自己的一些常见的代码写成可提供拓展和复用的工具类或者工具库,这些是这些优秀工程师的法宝. 我就先从String这个基本操作开始 ...
- 生产者-消费者中的缓冲区:BlockingQueue接口
BlockingQueue接口使用场景相信大家对生产者-消费者模式不陌生,这个经典的多线程协作模式,最简单的描述就是生产者线程往内存缓冲区中提交任务,消费者线程从内存缓冲区里获取任务执行.在生产者-消 ...
- 使用Caliburn.Micro系列1:新建项目并引入CM
一.WPF的几个MVVM模式实现 MVVMLight:小众的平民框架,实现简单粗暴. pass:最近更新在15年 官网: http://www.mvvmlight.net/ 最近一篇内容全面的好文: ...
- 让xamarin的Entry绑定时,支持Nullable类型
xamarin.forms默认情况下,如果属性是double?类型,绑定到Entry上,是无法实现双向绑定的, 可以自定义Converter实现双向绑定 public class NullableCo ...
- css滚动条样式修改
.activeMoreBankList{ height: 188px; overflow-y: auto;} /*滚动条样式*/.activeMoreBankList::-webkit-scrollb ...