广义 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题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. Vuex 状态管理的工作原理

    Vuex 状态管理的工作原理 为什么要使用 Vuex 当我们使用 Vue.js 来开发一个单页应用时,经常会遇到一些组件间共享的数据或状态,或是需要通过 props 深层传递的一些数据.在应用规模较小 ...

  2. 广播接收者案例_ip拨号器

    (1)定义一个类继承BroadCastReceiver public class OutGoingCallReceiver extends BroadcastReceiver { //当接收到外拨电话 ...

  3. 7.3php编译安装最终版

    进过无数次折腾后发现编译php安装的有些难, 问题可能是自己对linux不太了解吧. [root@third src]# cd php-7.3.6 [root@third php-7.3.6]# ./ ...

  4. 浅谈Java面向对象之抽象类(abstract)

    java语言,声明类时格式为: abstract class Db{} 说明Db类为抽象类.抽象方法是说没有方法的实现(方法体)此方法为抽象方法,只有抽象类和接口中才可以有抽象方法.简而言之,含有抽象 ...

  5. Pandas常用操作 - 删除指定行/指定列

    1. 删除指定行 new_df = df.drop(index='行索引') new_df = df.drop('行索引', axis='index') new_df = df.drop('行索引', ...

  6. Codeforces Round #756 (Div. 3)

    本场战绩:+451 题目如下: A. Make Even time limit per test 1 second memory limit per test 256 megabytes input ...

  7. [LeetCode]4.寻找两个正序数组的中位数(Java)

    原题地址: median-of-two-sorted-arrays 题目描述: 示例 1: 输入:nums1 = [1,3], nums2 = [2] 输出:2.00000 解释:合并数组 = [1, ...

  8. NeurIPS 2017 | TernGrad: Ternary Gradients to Reduce Communication in Distributed Deep Learning

    在深度神经网络的分布式训练中,梯度和参数同步时的网络开销是一个瓶颈.本文提出了一个名为TernGrad梯度量化的方法,通过将梯度三值化为\({-1, 0, 1}\)来减少通信量.此外,本文还使用逐层三 ...

  9. pytest(12)-Allure常用特性allure.attach、allure.step、fixture、environment、categories

    上一篇文章pytest Allure生成测试报告我们学习了Allure中的一些特性,接下来继续学习其他常用的特性. allure.attach allure.attach用于在测试报告中添加附件,补充 ...

  10. python虚拟环境与伪静态网页

    目录 一:python虚拟环境 1.本地虚拟环境 1.创建虚拟环境 2.venv表示虚拟环境标志 3.虚拟环境下载django 4.使用虚拟环境 二:伪静态(了解) 1.什么是伪静态网页? 2.为什么 ...