Tree Palindromes

Given a tree rooted at node 1 with N nodes, each is assigned a lower case latin character. Print the sum of length of longest palindrome substring from string generated by simple path from root to all other nodes i.e root to some other node (say x) will represent a string, find its lps length and add it to answer for all such nodes x. Then print final answer.

1 ≤ N ≤ 105

题解

翁文涛《回文树及其应用》。

回文树也可以有类似AC自动机的不全转移,不过这个补全是间接性的。

\(t=even\) 的时候要特殊处理。考虑实际意义(或者get_fail过程)可知 \(quick_{even}[c]=odd\)。

有了这个Trie上就好做了。

本题字符集较小,在插入的时候,没有必要将失配转移可持久化,直接开一个数组记录即可。时间复杂度 \(O(n\Sigma)\)。

CO int N=100000+10;
vector<int> to[N]; namespace PAM{
int str[N],n;
int tot;
int ch[N][26],trans[N][26],fa[N],len[N]; IN void init(){
memset(str,-1,sizeof str);
tot=1;
fa[0]=fa[1]=1;
len[0]=0,len[1]=-1;
fill(trans[0],trans[0]+26,1);
}
int extend(int p,int c){
if(str[n-len[p]-1]!=str[n]) p=trans[p][c];
if(!ch[p][c]){
int cur=++tot;
len[cur]=len[p]+2;
fa[cur]=ch[trans[p][c]][c];
ch[p][c]=cur;
copy(trans[fa[cur]],trans[fa[cur]]+26,trans[cur]);
trans[cur][str[n-len[fa[cur]]]]=fa[cur];
}
return ch[p][c];
}
} char tree[N];
int last[N],res[N];
LL ans; void dfs(int x,int fa){
PAM::str[++PAM::n]=tree[x]-'a';
last[x]=PAM::extend(last[fa],tree[x]-'a');
res[x]=max(res[fa],PAM::len[last[x]]);
ans+=res[x];
for(int i=0;i<(int)to[x].size();++i)
if(to[x][i]!=fa) dfs(to[x][i],x);
--PAM::n;
}
int main(){
int n=read<int>();
scanf("%s",tree+1);
for(int i=1;i<n;++i){
int u=read<int>(),v=read<int>();
to[u].push_back(v),to[v].push_back(u);
}
PAM::init();
dfs(1,0);
printf("%lld\n",ans);
return 0;
}

CodeChef Tree Palindromes的更多相关文章

  1. codechef Tree and Queries Solved

    题目链接: https://www.codechef.com/problems/IITK1P10 大概是:修改点值,求子树节点为0有多少个, DFS序后,BIT 询问,修改 ;    {        ...

  2. BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )

    树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...

  3. Codechef Observing the Tree

    Home » Practice(Hard) » Observing the Tree   https://www.codechef.com/problems/QUERY Observing the T ...

  4. 【CodeForces】914 E. Palindromes in a Tree 点分治

    [题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...

  5. Codechef Union on Tree

    Codechef Union on Tree https://www.codechef.com/problems/BTREE 简要题意: 给你一棵树,\(Q\)次询问,每次给出一个点集和每个点的\(r ...

  6. 【启发式搜索】Codechef March Cook-Off 2018. Maximum Tree Path

    有点像计蒜之道里的 京东的物流路径 题目描述 给定一棵 N 个节点的树,每个节点有一个正整数权值.记节点 i 的权值为 Ai.考虑节点 u 和 v 之间的一条简单路径,记 dist(u, v) 为其长 ...

  7. Codechef March Cook-Off 2018. Maximum Tree Path

    目录 题意 解析 AC_code @(Codechef March Cook-Off 2018. Maximum Tree Path) 题意 给你一颗\(n(1e5)\)个点有边权有点权的树,\(Mi ...

  8. [Codechef - ADITREE] Adi and the Tree - 树链剖分,线段树

    [Codechef - ADITREE] Adi and the Tree Description 树上每个节点有一个灯泡,开始所有灯泡都是熄灭的.每次操作给定两个数 \(a,b\) ,将 \(a,b ...

  9. codechef Prime Distance On Tree(树分治+FFT)

    题目链接:http://www.codechef.com/problems/PRIMEDST/ 题意:给出一棵树,边长度都是1.每次任意取出两个点(u,v),他们之间的长度为素数的概率为多大? 树分治 ...

随机推荐

  1. maven 向私服部署jar

    1.有源码的情况下 首先需要在要deploy的项目pom中添加私服地址 <distributionManagement> <repository> <id>nexu ...

  2. Allowed memory size of 134217728 bytes exhausted问题解决方法

    Allowed memory size of 134217728 bytes exhausted问题解决方法 php默认内存限制是128M,所以需要修改php.ini文件. 查找到memory_lim ...

  3. [原创]开源跨平台大型网络端口扫描器K8PortScan(支持批量A段/B段/C段/IP列表)

    0x000 K8PortScan Python版Cscan端口扫描器 Code: https://github.com/k8gege/K8PortScan K8portScan 1.0 Date: 2 ...

  4. Zookeeper的介绍与基本部署

    目录 简介 架构 安装 StandAlone模式 1. 安装 2. 修改配置 3. 启动 4. 验证 5. 基本用法 Distributed模式 1. 配置hosts 2. 配置zoo.cfg 3. ...

  5. [转帖]油猴脚本管理器 Tampermonkey v4.8 离线CRX安装包(谷歌浏览器版)

    https://www.52pojie.cn/thread-1010604-1-1.html 油猴脚本管理器 Tampermonkey v4.8 离线CRX安装包(谷歌浏览器版) 链接:https:/ ...

  6. 在spring中使用Hibernate5

    目录 1. Overview 2. Spring Integration 3. Maven Dependencies 4. Configuration 4.1. Using Java Configur ...

  7. 第十一节:Asp.Net Core 之内容缓存(IMemoryCache)

    1. 整体说明 ASP.NET Core 支持多种不同的缓存,最简单的缓存基于 IMemoryCache,它表示存储在 Web 服务器内存中的缓存,内存缓存可以存储任何对象,存储形式键值对,需要 .n ...

  8. cmdb知识总结

    cmdb面试 1.paramiko模块的作用与原理 2.cmdb是什么 3.为什么要开发CMDB? 4.你们公司有多少台服务器?物理机?虚拟机? 5.你的CMDB是如何实现的? 6.CMDB都用到了哪 ...

  9. FusionInsight大数据开发---Hive应用开发

    Hive应用开发 了解Hive的基本架构原理 掌握JDBC客户端开发流程 了解ODBC客户端的开发流程 了解python客户端的开发流程 了解Hcatalog/webHcat开发接口 掌握Hive开发 ...

  10. TeamViewer14试用版到期-怎么解决

    Teamviewer14提示试用期已到期怎么办? 问题分析: 出现这种问题,是因为在安装是选择了[公司/商务用途]或者[以上都是]这两个选项中的一个 解决方法: 1.退出TeamViewer远程软件, ...