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 常见的 ...
随机推荐
- 鼠标适配器Adapter
先来看看概念: 现在我们要写一个这样的东西,就是一个窗口,然后鼠标点一下就有一个小圆点,like this: 来我们来看代码: import java.awt.*; import java.util. ...
- Android中集成第三方支付
常见的第三方支付解决方案 支付宝支付 微信支付 银联支付 Ping++统一支付平台(需要继承服务器端和客户端) 短信支付 支付宝的集成流程 相关资料链接: 支付宝支付指引流程:支付指引流程 支付宝An ...
- [转]Windows Azure安全概述
本文转自:http://blogs.msdn.com/b/azchina/archive/2011/03/06/windows_5f00_azure_5f00_security_5f00_overvi ...
- FastDFS的简单使用
互联网中有海量的文件,比如电商网站有海量的图片文件,视频网站有海量的视频文件,如果使用传统的模式上传文件,肯定是不可取的.因此需要使用第三方服务器来存储图片 . 一.FastDFS简介 FastD ...
- CCF|路径解析|Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=ne ...
- R in action读书笔记(11)-第八章:回归-- 选择“最佳”的回归模型
8.6 选择“最佳”的回归模型 8.6.1 模型比较 用基础安装中的anova()函数可以比较两个嵌套模型的拟合优度.所谓嵌套模型,即它的一 些项完全包含在另一个模型中 用anova()函数比较 &g ...
- Python学习 Day 2-数据类型和变量
数据类型和变量 在Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080, ...
- Node.js——post方式提交的图片如何保存
https://www.cnblogs.com/bruce-gou/p/6399766.html 没有使用express框架,主要是对于 request 的监听,data的时候对数据进行保存,end的 ...
- [eclipse]的快捷键的设置
今天新解压了一个eclipse,发现alt+/提示的快捷键不好用了,开始比较犯懒,但是发现开发效率低下,总结几个eclipse下快捷方式的解决办法. 第一个解决没有一点提示的情况. 1.首先通过菜单打 ...
- C# 处理年月日提取时间
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...