Acyclic Organic Compounds
题意:
给一以1为根的字符树,给出每个节点的字符与权值,记 $diff_{x}$ 为从 $x$ 出发向下走,能走到多少不同的字符串,求问最大的
$diff_{x} + c_{x}$,并求有多少个 $diff_{x} + c_{x}$。
解法:
考虑$dfs$,从下到上启发式合并 $Trie$ 树,效率 $O(nlogn)$。
#include <iostream>
#include <cstdio>
#include <cstring> #define N 300010 using namespace std; struct edge
{
int x,to;
}E[N<<]; struct node
{
node *ch[];
int siz; node* init()
{
siz=;
memset(ch,,sizeof(ch));
return this;
};
}spT[N<<],*root[N]; int n,m,totn,totE,ans,ansv;
int fa[N],g[N],c[N];
char S[N]; void addedge(int x,int y)
{
E[++totE] = (edge){y,g[x]}; g[x]=totE;
E[++totE] = (edge){x,g[y]}; g[y]=totE;
} #define p E[i].x node* merge(node *p1,node *p2)
{
if(p1->siz < p2->siz) swap(p1,p2);
for(int t=;t<;t++)
if(p2->ch[t])
{
if(!p1->ch[t]) p1->ch[t]=p2->ch[t];
else p1->ch[t] = merge(p1->ch[t], p2->ch[t]);
}
p1->siz=;
for(int t=;t<;t++)
if(p1->ch[t]) p1->siz+=p1->ch[t]->siz;
return p1;
} void dfs(int x)
{
int tmp=S[x]-'a';
root[x]=spT[++totn].init();
root[x]->ch[tmp]=spT[++totn].init();
for(int i=g[x];i;i=E[i].to)
if(p!=fa[x])
{
fa[p]=x;
dfs(p);
}
for(int i=g[x];i;i=E[i].to)
if(p!=fa[x])
root[x]->ch[tmp] = merge(root[x]->ch[tmp],root[p]);
root[x]->siz = root[x]->ch[tmp]->siz;
if(root[x]->siz+c[x] > ansv)
{
ansv = root[x]->siz+c[x];
ans=;
}
else if(root[x]->siz+c[x] == ansv) ans++;
} int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) g[i]=;
totE=;
for(int i=;i<=n;i++) scanf("%d",&c[i]);
S[]='*';
scanf("%s",S+);
ans=;
totn=ansv=;
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
}
fa[]=;
dfs();
cout << ansv << endl << ans << endl;
}
return ;
}
Acyclic Organic Compounds的更多相关文章
- 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 ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)
trie合并的裸题...因为最多只有n个点,所以最多合并n次,复杂度$O(N*26)$. #include<iostream> #include<cstring> #inclu ...
- CF601D:Acyclic Organic Compounds
给n<=300000的树,每个点上有一个字母,一个点的权值为:从该点出发向下走到任意节点停下形成的不同字符串的数量,问最大权值. 题目本身还有一些奇怪要求在此忽略.. Trie合并的模板题. # ...
- 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的数取 ...
- Coupled model
常见的coupled models phase English paper WRF-Chem mechanism public data 一些重要的结论 干空气的状态方程 ECWMF驱动WRF 常见的 ...
- skipping the actual organic impact moderation supplied
The most recent running footwear design has gone out. The high cost is actually $150. Expert sports ...
- algorithm@ Shortest Path in Directed Acyclic Graph (O(|V|+|E|) time)
Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths fr ...
随机推荐
- GoogleFusionTablesAPI初探地图与云计算
http://developer.51cto.com/art/200906/129324.htm http://yexiaochai.iteye.com/blog/1893735 http://yex ...
- caffe搭建--vs2015+caffe+python3.5编译环境的搭建
修改build_win.cmd如下: @echo off @setlocal EnableDelayedExpansion :: Default values if DEFINED APPVEYOR ...
- lua 字符串处理
匹配模式(pattern) . 任何单个字符 %a 任何字母 %c 任何控制字符 %d 任何数字 %g 任何除空白符外的可打印字符 %l 所有小写字母 %p 所有标点符号 %s 所有空白字符 %u 所 ...
- HTML marquee标签
marquee语法 <marquee></marquee> 实例一<marquee>Hello, World</marquee> marquee常 ...
- (Vue)vue模板语法
Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据.Vue.js 的核心是一个允许你采用简洁的模板语法来声明式的将数据渲染进 DOM 的系统. ...
- 在Mac OS中配置CMake的详细图文教程http://blog.csdn.net/baimafujinji/article/details/78588488
CMake是一个比make更高级的跨平台的安装.编译.配置工具,可以用简单的语句来描述所有平台的安装(编译过程).并根据不同平台.不同的编译器,生成相应的Makefile或者project文件.本文主 ...
- go echo studygolang ___go_build_myT_go__1_.exe
https://github.com/studygolang/studygolang [stat]; 用户在线数据存到哪里:redis -> 表示存入 redis,这样支持多机部署; onlin ...
- 在iOS平台使用ffmpeg解码h264视频流(转)
在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,ffmpeg提供avformat_open_input接口,直接将文件路径或UR ...
- listview 没数据内容时显示一个提示文本
listview和textview 1:1.listview无内容的时候本身是不显示的,所以textview会显示 getlistview获取系统定义的listview
- iOS 打开应用与系统功能的调用
[[UIApplication sharedApplication] openURL:url]; 通过给url不同的值,可以实现调用系统自带 电话/短信/邮箱/浏览器/... 1.调用 电话pho ...