题目大意:给你一棵$n$个点的树,最多有$20$个叶子节点,问共有几个不同的子串

题解:广义$SAM$,对每个叶子节点深搜一次,每个节点的$lst$设为这个节点当时的父亲,这样就可以时建出来的$SAM$含有所有的字串

卡点:

C++ Code:

#include <cstdio>
#include <iostream> #define maxn 100010
int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxn << 1];
int ind[maxn];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {a, head[b]}; head[b] = cnt;
} int w[maxn]; namespace SAM {
#define N (maxn * 22 << 1)
int lst = 1, idx = 1;
int R[N], fail[N], nxt[N][10];
void append(int ch) {
int p = lst, np = lst = ++idx; R[np] = R[p] + 1;
for (; p && !nxt[p][ch]; p = fail[p]) nxt[p][ch] = np;
if (!p) fail[np] = 1;
else {
int q = nxt[p][ch];
if (R[p] + 1 == R[q]) fail[np] = q;
else {
int nq = ++idx;
R[nq] = R[p] + 1, fail[nq] = fail[q], fail[q] = fail[np] = nq;
std::copy(nxt[q], nxt[q] + 10, nxt[nq]);
for (; nxt[p][ch] == q; p = fail[p]) nxt[p][ch] = nq;
}
}
}
void dfs(int u, int fa = 0) {
append(w[u]);
int tmp = lst;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa) dfs(v, u), lst = tmp;
}
}
long long query() {
long long ans = 0;
for (int i = 2; i <= idx; i++) ans += R[i] - R[fail[i]];
return ans;
}
#undef N
} int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", w + i);
for (int i = 1, a, b; i < n; i++) {
scanf("%d%d", &a, &b);
addedge(a, b); ind[a]++, ind[b]++;
}
for (int i = 1; i <= n; i++) if (ind[i] == 1) {
SAM::lst = 1;
SAM::dfs(i);
}
printf("%lld\n", SAM::query());
return 0;
}

  

[洛谷P3346][ZJOI2015]诸神眷顾的幻想乡的更多相关文章

  1. 洛谷P3346 [ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)

    题意 题目链接 Sol 广义SAM的板子题. 首先叶子节点不超过20,那么可以直接对每个叶子节点为根的子树插入到广义SAM中. 因为所有合法的答案一定是某个叶子节点为根的树上的一条链,因此这样可以统计 ...

  2. Luogu P3346 [ZJOI2015]诸神眷顾的幻想乡 广义SAM 后缀自动机

    题目链接 \(Click\) \(Here\) 真的是好题啊-不过在说做法之前先强调几个自己总是掉的坑点. 更新节点永远记不住往上跳\(p = fa[p]\) 新建节点永远记不住\(len[y] = ...

  3. P3346 [ZJOI2015]诸神眷顾的幻想乡

    思路 注意到叶子节点(度数为1)只有20个,可以分别以这20个节点为根,把所有子串插入SAM中,统计最后的本质不同的子串个数 所以就是广义SAM了 然后注意要判断一下有无重复插入 代码 #includ ...

  4. BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1017  Solved: 599[Submit][S ...

  5. bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 对[广义后缀自动机]的一些理解

    先说一下对后缀自动机的理解,主要是对构造过程的理解. 构造中,我们已经得到了前L个字符的后缀自动机,现在我们要得到L+1个字符的后缀自动机,什么需要改变呢? 首先,子串$[0,L+1)$对应的状态不存 ...

  6. 【BZOJ 3926】 [Zjoi2015]诸神眷顾的幻想乡 (广义SAM)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 974  Solved: 573 Descriptio ...

  7. 字符串(广义后缀自动机):BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 843  Solved: 510[Submit][St ...

  8. BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 [广义后缀自动机 Trie]

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1124  Solved: 660[Submit][S ...

  9. 【BZOJ3926】[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机

    [BZOJ3926][Zjoi2015]诸神眷顾的幻想乡 Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝 ...

随机推荐

  1. swoole 相关

    安装虚拟机 VMware Workstation Pro 安装CentOS CentOS-7-x86_64-Minimal-1708.iso 安装FinalShell 教程地址 安装lnmp 教程地址 ...

  2. Android 9 Pie震撼来袭 同步登陆WeTest

    WeTest 导读 2018年8月7日,Google对外发布最新 Android 9.0 正式版系统,并宣布系统版本Android P 被正式命名为代号“Pie”,最新系统已经正式推送包括谷歌Pixe ...

  3. git 操作几个命令

     git clone ssh://lijianfeng@192.168.1.246:29418/GMGameSDK压栈:git stash查状态:git status切换到要修改的提交:git reb ...

  4. mysql 开启远程连接

    如图,修改mysql数据库中user表中的User字段为root的host为%,然后重新启动mysql服务即可让远程桌面连接本地.

  5. java 泛型历史遗留问题

    Map<String,Integer> hashMap = new HashMap<String,Integer>(); hashMap.put(); // hashMap.p ...

  6. 感觉总结了一切python常见知识点,可直接运行版本

    #encoding=utf-8#http://python.jobbole.com/85231/#作用域a=1def A(a): a=2 print 'A:',a def B(): print 'B: ...

  7. 获取ip地址以及获取城市等信息

    class Program { static void Main(string[] args) { string ip = GetIP(); if (ip != null) { string city ...

  8. C++11 type_traits 之is_same源码分析

    请看源码: template<typename _Tp, _Tp __v> struct integral_constant { static const _Tp value = __v; ...

  9. 82. Single Number [easy]

    Description Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1, ...

  10. 机器学习介绍(introduction)-读书笔记-

    一,什么是机器学习 第一个机器学习的定义来自于 Arthur Samuel.他定义机器学习为,在进行特定编程的情况下,给予计算机学习能力的领域.Samuel 的定义可以回溯到 50 年代,他编写了一个 ...