zoukankan      html  css  js  c++  java
  • bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机模板

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 4000005
    using namespace std;
    int n,v[N],du[N],head[N],ver[N],nxt[N],tot;
    void add(int a,int b)
    {
    	tot++;nxt[tot]=head[a];head[a]=tot;ver[tot]=b;return ;
    }
    int now[N],root,fa[N],ch[N][11],cnt,l[N];
    void insert(int p,int c)
    {
    	int np=++cnt;l[np]=l[p]+1;
    	while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
    	if(!p)fa[np]=1;
    	else
    	{
    		int q=ch[p][c];
    		if(l[q]==l[p]+1)fa[np]=q;
    		else
    		{
    			int nq=++cnt;l[nq]=l[p]+1;
    			fa[nq]=fa[q];
    			fa[np]=fa[q]=nq;
    			for(int i=0;i<10;i++)ch[nq][i]=ch[q][i];
    			while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];
    		}
    	}
    }
    void dfs(int x,int f,int y)
    {
    	now[x]=y;
    	for(int i=head[x];i;i=nxt[i])
    	{
    		if(ver[i]==f)continue;
    		insert(y,v[ver[i]]);
    		dfs(ver[i],x,ch[y][v[ver[i]]]);
    	}
    }
    int main()
    {
    	int c;
    	scanf("%d%d",&n,&c);
    	for(int i=1;i<=n;i++)scanf("%d",&v[i]);
    	int t1,t2;
    	for(int i=1;i<n;i++)
    	{
    		scanf("%d%d",&t1,&t2);
    		add(t1,t2);add(t2,t1);
    		du[t1]++;du[t2]++;
    	}
    	cnt=1;
    	for(int i=1;i<=n;i++)if(du[i]==1)add(0,i);
    	dfs(0,-1,1);
    	long long ans=0;
    	for(int i=2;i<=cnt;i++)ans+=l[i]-l[fa[i]];
    	printf("%lld
    ",ans);
    	return 0;
    }
    

      

  • 相关阅读:
    [HNOI2004]L语言
    快速沃尔什变换FWT
    [BZOJ1486][HNOI2009]最小圈
    [BZOJ4819][SDOI2017]新生舞会
    [POJ2976]Dropping tests
    CTSC2018&APIO2018游记
    [Luogu3769][CH弱省胡策R2]TATT
    [BZOJ3489]A simple rmq problem
    [BZOJ4066]简单题
    [BZOJ2648]SJY摆棋子
  • 原文地址:https://www.cnblogs.com/ezyzy/p/6739584.html
Copyright © 2011-2022 走看看