题解:P4218 [CTSC2010] 珠宝商
题解
考虑暴力怎么做?我们枚举树上的点作为起点,然后在
考虑我们要统计树上所有的路径于是点分治启动!若当前分治中心为
对
假设现在在
- 当前串长等于
\text{maxlen} ,说明我再往前加一个字符就会跳到其他点,我们就需要找一个点v 满足fa_v=u 且S[\text{rpos}_v-\text{len}_u]=c ,其中\text{rpos}_v 表示v 的\text{endpos} 中任意一点(任一点都是等价的)。如果有就能够跳,否则新串就不是S 的子串了。 - 如果当前串长小于
\text{maxlen} ,我们就看S[\text{rpos}_u-|T|] 是否等于c ,其中T 为原串,|T| 表示其长度。如果不等于就说明新串就不是S 的子串,否则不变。
我们对所有的情况一都预处理出来,存在
最后还要讲一下容斥。就是说我们的
分治加上遍历树是
因为在 复杂度我不太会证只会感性理解现在再次分析复杂度。
时间复杂度
我们知道每次分治点数会减半,考虑第
最后要注意的就是容斥的时候也要考虑根号分治,否则如果图是菊花图就 T 飞了。
代码实现细节
因为代码太长就讲一下实现。因为要维护
然后就是普通的暴力和容斥的暴力应该怎么写。普通的暴力就是外层一个
代码
/*
* @Author: Nekopedia
* @Date: 2025-05-12 14:24:26
* @Last Modified by: Nekopedia
* @Last Modified time: 2025-05-12 18:21:50
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5, inf = 1e9;
const ll INF = 2e18;
int hd[N], cnt, n, m, sn;
struct edge{int nxt, to;}e[N];
inline void add(int u, int v){e[++cnt] = {hd[u], v}; hd[u] = cnt;}
char s1[N], s2[N], a[N];
ll ans;
int rt, mi = inf, sz[N], sum[N];
bitset < N > tg;
struct sam{
vector < int > g[N];
int ch[N][26], fa[N], son[N][26], len[N], sz[N], pos[N], rp[N];
int tot, ep, s[N];
ll cnt[N];
sam(){tot = ep = 1;}
inline void clr(){for(int i = 1; i <= tot; ++i)cnt[i] = 0;}
void ins(int c){
int u = ep; ep = ++tot;
rp[ep] = len[ep] = len[u] + 1; sz[ep] = 1;
for(; u and ! ch[u][c]; u = fa[u])ch[u][c] = ep;
if(! u)return fa[ep] = 1, void();
int v = ch[u][c];
if(len[u] + 1 == len[v])return fa[ep] = v, void();
int x = ++tot; len[x] = len[u] + 1;
fa[x] = fa[v]; fa[v] = fa[ep] = x;
for(int i = 0; i < 26; ++i)ch[x][i] = ch[v][i];
for(; u and ch[u][c] == v; u = fa[u])ch[u][c] = x;
}
void dfs(int u){
for(int v : g[u]){
dfs(v);
sz[u] += sz[v]; rp[u] = rp[v];
son[u][s[rp[v] - len[u]]] = v;
}
}
void init(){
for(int i = 1; i <= m; ++i)ins(s[i]), pos[i] = ep;
for(int i = 2; i <= tot; ++i)g[fa[i]].push_back(i);
dfs(1);
}
void upd(int u, int ff, int p, int l){
if(len[p] == l)p = son[p][a[u] - 'a'];
else if(s[rp[p] - l] ^ (a[u] - 'a'))p = 0;
if(! p)return; ++cnt[p];
for(int i = hd[u]; i; i = e[i].nxt){
int v = e[i].to; if(v == ff or tg[v])continue;
upd(v, u, p, l + 1);
}
}
void pd(int u){cnt[u] += cnt[fa[u]]; for(int v : g[u])pd(v);}
void pt(){
cout << tot << endl;
for(int i = 1; i <= tot; ++i)cout << sz[i] << ' ' << rp[i] << endl;
for(int i = 1; i <= m; ++i)cout << s[i]; cout << endl;
}
}sp, sf;
void getrt(int u, int fa, int tot){
sz[u] = 1; int ma = - inf;
for(int i = hd[u]; i; i = e[i].nxt){
int v = e[i].to; if(v == fa or tg[v])continue;
getrt(v, u, tot); sz[u] += sz[v];
ma = max(ma, sz[v]);
}
ma = max(ma, tot - sz[u]);
if(ma < mi)mi = ma, rt = u;
}
void dfs1(int u, int fa, int p, ll o){
p = sp.ch[p][a[u] - 'a']; if(! p)return;
ans += o * sp.sz[p];
for(int i = hd[u]; i; i = e[i].nxt){
int v = e[i].to; if(v == fa or tg[v])continue;
dfs1(v, u, p, o);
}
}
void dfs2(int u, int fa){
dfs1(u, 0, 1, 1);
for(int i = hd[u]; i; i = e[i].nxt){
int v = e[i].to; if(v == fa or tg[v])continue;
dfs2(v, u);
}
}
void dfs3(int u, int fa){
sum[u] = 1;
for(int i = hd[u]; i; i = e[i].nxt){
int v = e[i].to; if(v == fa or tg[v])continue;
dfs3(v, u); sum[u] += sum[v];
}
}
int st;
void dfs4(int u, int fa, int p, int l){
if(sp.len[p] == l)p = sp.son[p][a[u] - 'a'];
else if(sp.s[sp.rp[p] - l] ^ (a[u] - 'a'))p = 0;
dfs1(st, rt, p, - 1);
for(int i = hd[u]; i; i = e[i].nxt){
int v = e[i].to; if(v == fa or tg[v])continue;
dfs4(v, u, p, l + 1);
}
}
void sol(int u, int fa, ll o){
sp.clr(); sf.clr();
if(fa)
sp.upd(u, 0, sp.son[1][a[fa] - 'a'], 1),
sf.upd(u, 0, sf.son[1][a[fa] - 'a'], 1);
else sp.upd(u, 0, 1, 0), sf.upd(u, 0, 1, 0);
sf.pd(1); sp.pd(1);
for(int i = 1; i <= m; ++i)ans += o * sp.cnt[sp.pos[i]] * sf.cnt[sf.pos[m - i + 1]];
}
void dfs(int u, int tot){
if(tot < sn)return dfs2(u, 0), void();
tg.set(u); sol(u, 0, 1); dfs3(u, 0);
for(int i = hd[u]; i; i = e[i].nxt){
int v = e[i].to; if(tg[v])continue;
if(sz[v] < sn)st = v, dfs4(v, u, sp.son[1][a[u] - 'a'], 1); else sol(v, u, - 1);
mi = inf, rt = 0; getrt(v, u, sum[v]); dfs(rt, sum[v]);
}
}
signed main(){
ios :: sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; sn = 600;
for(int i = 1, u, v; i < n; ++i)cin >> u >> v, add(u, v), add(v, u);
cin >> a + 1 >> s1 + 1;
for(int i = 1; i <= m; ++i)sp.s[i] = sf.s[m - i + 1] = s1[i] - 'a', s2[m - i + 1] = s1[i];
sp.init(); sf.init();
getrt(1, 0, n); dfs(rt, n);
cout << ans;
return 0;
}