后缀自动机 【Zjoi2015】诸神眷顾的幻想乡 bzoj3926
来源:互联网 发布:c语言 extern static 编辑:程序博客网 时间:2024/04/28 06:11
题目大意:
给定一棵n(1<=n<=100000)个点的树,每个点上有一种颜色,颜色数不超过10。
问这个树上有多上个本质不同的链。
只与一条边相连的点不超过20个。
题目分析:(广义后缀自动机)
广义后缀自动机啥的听起来就很高端,但是其实就是把几个串的的root捏在一起做后缀自动机……
因为只与一条边相连的点不超过20个,所以建20个广义后缀自动机,在插入点的到时候顺便维护一下答案。也就是20遍dfs,时间复杂度O(n)。
注意事项: 答案输出用long long(我这个不长记性的大二货QAQ……)
代码如下:
#include<cstdio>#include<cstring>#define N 120000using namespace std;int n,m;long long ans;int fir[N],nes[N<<1],v[N<<1],top=1,q[N];int in_d[N];void edge(int x,int y){ top++; v[top]=y; nes[top]=fir[x]; fir[x]=top; in_d[y]++; return;}#define edge(x,y) edge(x,y),edge(y,x)struct SAM{ SAM *son[10],*fa; int max_len; SAM(int _=0):fa(0x0),max_len(_){memset(son,0,sizeof(son));}}*root=new SAM,*last[N];SAM* extend(int x,SAM *last){ SAM *p=last; SAM *np=new SAM(p->max_len+1); while(p && !p->son[x]) p->son[x]=np,p=p->fa; if(!p) np->fa=root; else { SAM *q=p->son[x]; if(p->max_len+1==q->max_len) np->fa=q; else { SAM *nq=new SAM(p->max_len+1); ans-=q->max_len-q->fa->max_len; nq->fa=q->fa; memcpy(nq->son,q->son,sizeof(nq->son)); q->fa=nq; np->fa=nq; for(;p && p->son[x]==q;p=p->fa) p->son[x]=nq; ans+=q->max_len-q->fa->max_len; ans+=nq->max_len-nq->fa->max_len; } } ans+=np->max_len-np->fa->max_len; return np;}void dfs(int c,int father){ last[c]=extend(q[c],last[father]); for(int t=fir[c];t;t=nes[t]) if(v[t]!=father) dfs(v[t],c); return;}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&q[i]); for(int i=1,x,y;i<n;i++) { scanf("%d%d",&x,&y); edge(x,y); } last[0]=root; for(int i=1;i<=n;i++) if(in_d[i]==1) dfs(i,0); printf("%lld\n",ans); return 0;}
0 0
- [BZOJ3926]ZJOI2015诸神眷顾的幻想乡|后缀自动机
- 【BZOJ3926】【Zjoi2015】诸神眷顾的幻想乡 后缀自动机
- 【bzoj3926】[Zjoi2015]诸神眷顾的幻想乡 后缀自动机
- 后缀自动机 【Zjoi2015】诸神眷顾的幻想乡 bzoj3926
- [BZOJ3926][Zjoi2015]诸神眷顾的幻想乡-后缀自动机
- 【BZOJ3926】【Zjoi2015】诸神眷顾的幻想乡 广义后缀自动机
- BZOJ3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
- bzoj3926:[Zjoi2015]诸神眷顾的幻想乡(后缀自动机)
- BZOJ3926 [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
- bzoj3926[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
- BZOJ3926:[Zjoi2015]诸神眷顾的幻想乡 (后缀自动机)
- bzoj3926 [Zjoi2015]诸神眷顾的幻想乡(广义后缀自动机)
- bzoj3926 诸神眷顾的幻想乡 后缀自动机
- 【ZJOI2015】【BZOJ3926】诸神眷顾的幻想乡
- 【bzoj3926】[Zjoi2015]诸神眷顾的幻想乡
- bzoj3926: [Zjoi2015]诸神眷顾的幻想乡
- bzoj3926【ZJOI2015】诸神眷顾的幻想乡
- 【bzoj3926】[Zjoi2015]诸神眷顾的幻想乡
- Android动画之逐帧动画(FrameAnimation)详解
- 命令行聊天室
- tcp python socket client
- mongoVUE 报 MongoAuthenticationException
- PAT甲级1016
- 后缀自动机 【Zjoi2015】诸神眷顾的幻想乡 bzoj3926
- android项目中常用技术-自我总结二
- spring集合
- Spring MVC 解读——@Autowired、@Controller、@Service从原理层面来分析
- 启动hbase输出ignoring option PermSize=128m; support was removed in 8.0告警信息
- 【matlab】样条函数:csapi、spap2和augknt的使用
- 洛谷 P1019 单词接龙
- Spring -- 依赖注入源码分析
- Tricks in decide the index in the binary search with duplicate elements