zoukankan      html  css  js  c++  java
  • BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡

    3926: [Zjoi20150]诸神眷顾的幻想乡

    Time Limit: 10 Sec  Memory Limit: 512 MB
    Submit: 438  Solved: 273

    Description

     幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日。 

    粉丝们非常热情,自发组织表演了一系列节目给幽香看。幽香当然也非常高兴啦。 
    这时幽香发现了一件非常有趣的事情,太阳花田有n块空地。在过去,幽香为了方便,在这n块空地之间修建了n-1条边将它们连通起来。也就是说,这n块空地形成了一个树的结构。 
    有n个粉丝们来到了太阳花田上。为了表达对幽香生日的祝贺,他们选择了c中颜色的衣服,每种颜色恰好可以用一个0到c-1之间的整数来表示。并且每个人都站在一个空地上,每个空地上也只有一个人。这样整个太阳花田就花花绿绿了。幽香看到了,感觉也非常开心。 
    粉丝们策划的一个节目是这样的,选中两个粉丝A和B(A和B可以相同),然后A所在的空地到B所在的空地的路径上的粉丝依次跳起来(包括端点),幽香就能看到一个长度为A到B之间路径上的所有粉丝的数目(包括A和B)的颜色序列。一开始大家打算让人一两个粉丝(注意:A,B和B,A是不同的,他们形成的序列刚好相反,比如红绿蓝和蓝绿红)都来一次,但是有人指出这样可能会出现一些一模一样的颜色序列,会导致审美疲劳。 
    于是他们想要问题,在这个树上,一共有多少可能的不同的颜色序列(子串)幽香可以看到呢? 
    太阳花田的结构比较特殊,只与一个空地相邻的空地数量不超过20个。 

    Input

     第一行两个正整数n,c。表示空地数量和颜色数量。 

    第二行有n个0到c-1之间,由空格隔开的整数,依次表示第i块空地上的粉丝的衣服颜色。(这里我们按照节点标号从小到大的顺序依次给出每块空地上粉丝的衣服颜色)。 
    接下来n-1行,每行两个正整数u,v,表示有一条连接空地u和空地v的边。 

    Output

     一行,输出一个整数,表示答案。 

    Sample Input

    7 3
    0 2 1 2 1 0 0
    1 2
    3 4
    3 5
    4 6
    5 7
    2 5

    Sample Output

    30

    HINT

    对于所有数据,1<=n<=100000, 1<=c<=10。 


    对于15%的数据,n<=2000。 

    另有5%的数据,所有空地都至多与两个空地相邻。 

    另有5%的数据,除一块空地与三个空地相邻外,其他空地都分别至多与两个空地相邻。 

    另有5%的数据,除某两块空地与三个空地相邻外,其他空地都分别至多与两个空地相邻
     
    解题:广义后缀自动机,陈老师的语文水平高
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 100010;
     5 vector<int>g[maxn];
     6 int du[maxn];
     7 struct SAM {
     8     struct node {
     9         int son[10],f,len;
    10         void init(int _len = 0) {
    11             memset(son,-1,sizeof son);
    12             f = -1;
    13             len = _len;
    14         }
    15     } e[4000010];
    16     int tot,last;
    17     int newnode(int len = 0) {
    18         e[tot].init(len);
    19         return tot++;
    20     }
    21     void init() {
    22         tot = last = 0;
    23         newnode();
    24     }
    25     int extend(int p,int c) {
    26         int np = newnode(e[p].len + 1);
    27         while(p != -1 && e[p].son[c] == -1) {
    28             e[p].son[c] = np;
    29             p = e[p].f;
    30         }
    31         if(p == -1) e[np].f = 0;
    32         else {
    33             int q = e[p].son[c];
    34             if(e[p].len + 1 == e[q].len) e[np].f = q;
    35             else {
    36                 int nq = newnode();
    37                 e[nq] = e[q];
    38                 e[nq].len = e[p].len + 1;
    39                 e[np].f = e[q].f = nq;
    40                 while(p != -1 && e[p].son[c] == q) {
    41                     e[p].son[c] = nq;
    42                     p = e[p].f;
    43                 }
    44             }
    45         }
    46         return np;
    47     }
    48     LL count(LL ret = 0) {
    49         for(int i = 1; i < tot; ++i)
    50             ret += e[i].len - e[e[i].f].len;
    51         return ret;
    52     }
    53 } sam;
    54 
    55 int color[maxn];
    56 void dfs(int u,int fa,int p){
    57     int x = sam.extend(p,color[u]);
    58     for(int i = g[u].size()-1; i >= 0; --i)
    59         if(g[u][i] != fa) dfs(g[u][i],u,x);
    60 }
    61 int main() {
    62     int n,c,u,v;
    63     while(~scanf("%d%d",&n,&c)) {
    64         sam.init();
    65         for(int i = 0; i <= n; ++i) {
    66             g[i].clear();
    67             du[i] = 0;
    68         }
    69         for(int i = 1; i <= n; ++i)
    70             scanf("%d",color + i);
    71         for(int i = 1; i < n; ++i){
    72             scanf("%d%d",&u,&v);
    73             ++du[u];
    74             ++du[v];
    75             g[u].push_back(v);
    76             g[v].push_back(u);
    77         }
    78         for(int i = 1; i <= n; ++i)
    79             if(du[i] == 1) dfs(i,0,0);
    80         printf("%lld
    ",sam.count());
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    《EffectiveJava中文第二版》 高清PDF下载
    《MoreEffectiveC++中文版》 pdf 下载
    《啊哈c语言》 高清 PDF 下载
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4912458.html
Copyright © 2011-2022 走看看