String (generalized suffix automaton): Bzoj 3926 [Zjoi2015] Fantasy Township of the Gods

Source: Internet
Author: User

3926: [Zjoi2015] The gods favor the fantasy township time limit:10 Sec Memory limit:512 MB
submit:843 solved:510
[Submit] [Status] [Discuss] Description

Fragrance is the most popular fantasy village sister, this day, is a delicate fragrance of the 2,600 birthday, countless fragrance fans to the fragrance of the sun in front of the flower field to celebrate the fragrance of the birthday.

The fans were so enthusiastic that they spontaneously organized a series of shows for the delicate fragrance to see. The fragrance of course is also very happy. At this time the delicate fragrance found a very interesting thing, the sun flower field has n blocks of vacant space. In the past, delicate fragrance for convenience, in the N-block between the construction of the n-1 edge to connect them together. In other words, the N-Block space forms the structure of a tree. N fans came to the sun flower field. In order to express their congratulations on the fragrance of the birthday, they chose the C color of the clothes, each color can be used in a 0 to c-1 between the integer to express. And everyone was standing on an empty ground, and there was only one person in every clearing. So the whole sun field is colorful. The fragrance saw, feel also very happy. The fans planned a show like this, select two fans A and B (A and B can be the same), then a in the open space to B in the path of the open space of fans jump up (including the end), the fragrance will be able to see a length of a to B on the path between the number of all fans (including A and b) color sequence At first, we were going to have one or two fans (note: A, B and b,a are different, they form exactly the opposite, such as red and green and blue and green), but it is pointed out that there may be some identical color sequences that can lead to aesthetic fatigue. So they want the question, in this tree, how many possible different color sequences (substrings) of the fragrance can be seen? The structure of the Sun flower field is special, the number of vacant spaces adjacent to only one vacant space is not more than 20. Input

The first row of two positive integers n,c. Indicates the number of vacant spaces and the number of colors.

The second line has n 0 to C-1, a space-separated integer, which in turn represents the color of the fan's clothes on the block I space. (Here we follow the node label from small to large in order to give each piece of space on the color of the fan's clothes). Next n-1 lines, each line of two positive integer u,v, indicating that there is a connection between the open space U and open space V side. Output

One line, outputting an integer that represents the answer.

Sample Input7 3
0 2 1 2 1 0 0
1 2
3 4
3 5
4 6
5 7
2 5Sample Output -HINT

For all data, 1<=n<=100000, 1<=c<=10.

For 15% of data, n<=2000. In another 5% of the data, all vacant lots are adjacent to two open spaces. Another 5% of the data, in addition to a vacant space and three vacant space adjacent to, the other vacant space is up to two vacant space adjacent.    Another 5% per cent of the data, in addition to a couple of open space and three vacant space adjacent to the other open space are up to two vacant space adjacent. Knowing to build a trie tree from each leaf node and merge it, I'll do it.
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=100010;6 Const intmaxm=2000010;7 Long Longans;8 BOOLVIS[MAXM];9 intN,SUMC,CNTE,C[MAXN];Ten intfir[maxn],nxt[maxn<<1],to[maxn<<1]; One voidAddedge (intAintb) { Anxt[++cnte]=Fir[a]; -fir[a]=Cnte; -to[cnte]=b; the } - intSt[maxm],topst; - intch[maxm][ One],POS[MAXM]; - intfa[maxm],ch[maxm][ One],LEN[MAXM]; + structsam{ -     intCnt,last; + SAM () { Acnt=last=1; at     } -     intInsert (intc) { -         intp=last,np=last=++cnt;len[np]=len[p]+1; -          while(P&&!ch[p][c]) ch[p][c]=np,p=Fa[p]; -         if(!p) fa[np]=1; -         Else{ in             intq=Ch[p][c]; -             if(len[p]+1==Len[q]) tofa[np]=Q; +             Else{ -                 intnq=++cnt;len[nq]=len[p]+1; thememcpy (Ch[nq],ch[q],sizeof(Ch[q])); *fa[nq]=fa[q];fa[q]=fa[np]=NQ; $                  while(ch[p][c]==q) ch[p][c]=nq,p=Fa[p];Panax Notoginseng             }     -         } theans+=len[last]-Len[fa[last]]; +         returnLast ; A     } the }sam; + structtrie{ -     intCnt,rt; $ Trie () { $Rt=cnt=1; -pos[rt]=1; -     } the     voidInsert (int&x,intp) { -vis[p]=true;Wuyi         if(!x) x=++CNT; the          for(intI=fir[p];i;i=Nxt[i]) -             if(!Vis[to[i]]) Wu Insert (Ch[x][c[to[i]]],to[i]); -     } About     voidConstruct (intx) { $          for(intI=0; i<sumc;i++) -             if(Ch[x][i]) { -sam.last=Pos[x]; -pos[ch[x][i]]=Sam. Insert (i); A             } +          for(intI=0; i<sumc;i++) the             if(Ch[x][i]) - Construct (Ch[x][i]); $     } the }trie; the intMain () { the #ifndef Online_judge theFreopen ("zjoi15_substring.in","R", stdin); -Freopen ("Zjoi15_substring.out","W", stdout); in #endif thescanf"%d%d",&n,&SUMC); the      for(intI=1; i<=n;i++) Aboutscanf"%d",&C[i]);  the      for(intI=1, a,b;i<n;i++){ thescanf"%d%d",&a,&b); the Addedge (A, b); + Addedge (b,a); -     }     the      for(intx=1, tot=0; x<=n;tot=0, X + +){Bayi          for(intI=fir[x];i;i=nxt[i]) tot+=1; the         if(tot==1) st[++topst]=x; the     } -      while(topst) { -memset (Vis,0,sizeof(Vis)); the trie. Insert (Ch[trie.rt][c[st[topst]]],st[topst]); thetopst-=1; the     } the trie. Construct (TRIE.RT); -printf"%lld\n", ans); the     return 0;  the}

String (generalized suffix automaton): Bzoj 3926 [Zjoi2015] Fantasy Township of the Gods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.