LGP3346题解
广义 SAM 比较简单的题/fad
题意:树上所有路径一共能够组成多少个本质不同子串?
并且数据保证最多只有20个叶子节点。
我们先来考虑一下一种特殊情况:
对于路径 \([u,v]\),\(u\) 是 \(v\) 的父亲或 \(v\) 是 \(u\) 的父亲。
此时做法很明显:将整棵树当做一颗 Trie 树,建立其广义 SAM,然后根据套路求值。
但是很明显这样并不行,因为树并不是一条链。
这题最多只有 20 个叶子节点,暗示我们将每个叶子节点都当做根节点一遍,这样就一定没有考虑漏的情况。
然后就变成广义 SAM 板子题了。。。
code:
#include<cstdio>
#include<queue>
const int M=1e5+5;
int n,m,tot,siz[M],col[M],lst[M*20];
long long ans;
struct Trie{
int chi[11];
}t[M*20];
struct Node{
int chi[11];
int f,len;
}SAM[M*40];
struct Edge{
int to;Edge*nx;
}e[M<<1],*h[M],*cnt=e;
inline void Add(const int&u,const int&v){
*cnt=(Edge){v,h[u]};h[u]=cnt++;
*cnt=(Edge){u,h[v]};h[v]=cnt++;
}
void qwq(int u,int q,int f){
for(Edge*E=h[u];E;E=E->nx){
int v=E->to;
if(v==f)continue;
if(!t[q].chi[col[v]])t[q].chi[col[v]]=++tot;
qwq(v,t[q].chi[col[v]],u);
}
}
void DFS(int u,int f){
if(siz[u]==1){
if(!t[0].chi[col[u]])t[0].chi[col[u]]=++tot;
qwq(u,t[0].chi[col[u]],0);
}
for(Edge*E=h[u];E;E=E->nx){
int v=E->to;
if(v==f)continue;
DFS(v,u);
}
}
inline int Insert(int lst,int s){
int q,p,nq,np;
p=lst;np=++tot;
SAM[np].len=SAM[p].len+1;
for(;p&&!SAM[p].chi[s];p=SAM[p].f)SAM[p].chi[s]=np;
if(!p)SAM[np].f=1;
else{
q=SAM[p].chi[s];
if(SAM[q].len==SAM[p].len+1)SAM[np].f=q;
else{
nq=++tot;
SAM[nq]=SAM[q];
SAM[np].f=SAM[q].f=nq;
SAM[nq].len=SAM[p].len+1;
for(;p&&SAM[p].chi[s]==q;p=SAM[p].f)SAM[p].chi[s]=nq;
}
}
ans+=SAM[np].len-SAM[SAM[np].f].len;
return np;
}
inline void MakeSAM(){
std::queue<int>q;
int u,s;
tot=lst[0]=1;q.push(0);
while(!q.empty()){
u=q.front();q.pop();
for(s=0;s<m;++s){
if(t[u].chi[s]){
lst[t[u].chi[s]]=Insert(lst[u],s);
q.push(t[u].chi[s]);
}
}
}
}
signed main(){
register int i,u,v;
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i)scanf("%d",col+i);
for(i=1;i<n;++i){
scanf("%d%d",&u,&v);
Add(u,v);++siz[u];++siz[v];
}
DFS(1,0);MakeSAM();
printf("%lld",ans);
}
LGP3346题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- SpringBeanUtils的部分方法类
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/12060553.html SpringBeanUtils的部分方法类: import java. ...
- endl与\n的用法区别
学习C++的时候,老师说换行有两种写法. 1 //方法一 2 3 std::cout<<"你好!\n李华"; 4 5 //方法二 6 7 std::cout<&l ...
- DbUnit入门实战
原文地址: http://yangzb.iteye.com/blog/947292 相信做过单元测试的人都会对JUnit 非常的熟悉了, 今天要介绍的DbUnit(http://dbunit.sour ...
- 关于 BSGS 以及 ExBSGS 算法的理解
BSGS 引入 求解关于\(X\)的方程, \[A^X\equiv B \pmod P \] 其中\(Gcd(A,P)=1\) 求解 我们令\(X=i*\sqrt{P}-j\),其中\(0<=i ...
- 编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构
一.环境准备 两台主机: httpd+php(fastcgi模式) mariadb 服务器 软件版本: mariadb-10.2.40-linux-x86_64.tar.gz apr-1.7.0.ta ...
- JavaWeb项目中斜杠(/)表示web工程、webapps的场景
"/"代表当前web工程的常见应用场景 ①.ServletContext.getRealPath(String path)获取资源的绝对路径 /** * 1.ServletCont ...
- HTTP缓存协议实战
一.什么是缓存 缓存,又称作Cache,我们把临时存储数据的地方叫做缓存池,缓存池里面放的数据就叫做缓存.当用户需要使用这些数据,首先在缓存中寻找,如果找到了则直接使用.如果找不到,则再去其他数据源中 ...
- opencv笔记--stitching模块
opencv 提供了全景图像拼接的所有实现,包括: 1)stitching 模块提供了图像拼接过程中所需要的基本元素,该模块主要依赖于 features2d 模块: 2)提供了 stitching_d ...
- ELK-EFK-v7.12.0日志平台部署
ELK和EFK是什么 ELK和EFK是四个开源产品的组合: Elasticsearch 一个基于Lucene搜索引擎的NoSQL数据库 Logstatsh 一个日志管道工具,接受数据输入,执行数据转换 ...
- Jenkins项目迁移JOB
Jenkins项目迁移JOB:从旧的迁移到新的Jenkins上 北京介绍 今天由于迁移Jenkins服务器,需要完全拷贝Jenkins里的Job. 经实验,直接在服务器上移动Jobs目录下的文件即可. ...