题目大意:给你一棵$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 ", SAM::query()); return 0; }