Tag: print represents output char read signed Ann Amp const
3926: [Zjoi2015] The gods favor the fantasy township time limit:10 Sec Memory limit:512 MB
submit:1017 solved:599
[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
Source [Submit] [Status] [Discuss]
Otz Chen's enigmatic language skills ...
By careful observation of the last line of this short film, it is not difficult to see that this wonderful field formed a tree with a maximum of only 20 leaf nodes (which I did not see for a while).
So from this up to 20 nodes, to root DFS, set up a generalized automaton, then each path on the tree is a sub-string that can be accepted by Sam, the answer is very different in Sam the number of substrings, accumulate the longest path difference.
1#include <bits/stdc++.h>2 3 /*Myscanner*/4 5 #defineSiz 10246 7InlineintGet_c (void)8 {9 Static CharBuf[siz];Ten Static Char*head = buf +siz; One Static Char*tail = buf +siz; A - if(Head = =tail) -Fread (head = buf,1, Siz, stdin); the - return*head++; - } - +InlineintGet_i (void) - { +RegisterintRET =0; ARegisterintNeg =false; atRegisterintbit =Get_c (); - - for(; bit < -; bit =Get_c ()) - if(bit = ='-') Neg ^=true; - - for(; bit > -; bit =Get_c ()) inRET = RET *Ten+ Bit-'0'; - to returnNeg? -Ret:ret; + } - the Const intMAXN =2000100; * $ /*Automaton*/Panax Notoginseng - intTail =1; the intROOT[MAXN]; + intSTEP[MAXN]; A intFAIL[MAXN]; the intnext[maxn][ the]; + -InlineintExtendintPintc) $ { $ if(Next[p][c] && step[next[p][c]] = = Step[p] +1) - returnNext[p][c]; - intt = tail++; theSTEP[T] = Step[p] +1; - while(P &&!)Next[p][c])WuyiNext[p][c] = t, p =Fail[p]; the if(P) - { Wu intQ =Next[p][c]; - if(Step[q] = = Step[p] +1) AboutFAIL[T] =Q; $ Else - { - intK = tail++; -FAIL[K] =Fail[q]; AFAIL[Q] = fail[t] =K; +STEP[K] = Step[p] +1; thememcpy (Next[k], next[q],sizeof(Next[k])); - while(P && next[p][c] = =q) $Next[p][c] = k, p =Fail[p]; the } the } the Else theFAIL[T] =1; - returnT; in } the the /*GRAPH*/ About the intN; the intm; the inttot; + intHD[MAXN]; - intNT[MAXN]; the intTO[MAXN];Bayi intCNT[MAXN]; the intCOL[MAXN]; the intVIS[MAXN]; - intQUE[MAXN]; - theInlinevoidAddedge (intXinty) the { theNt[++tot] = hd[x]; To[tot] = y; HD[X] =tot; theNt[++tot] = Hd[y]; To[tot] = x; Hd[y] =tot; - } the the voidDasointUintf) the {94Root[u] =extend (root[f], col[u]); the for(inti = Hd[u]; I i =Nt[i]) the if(To[i]! =f) DFS (To[i], u); the }98 About /*MAIN FUNC*/ - 101Signed Main (void)102 {103n =get_i ();104m =get_i (); the 106 for(inti =1; I <= N; ++i)107Col[i] =get_i ();108 109 for(inti =1; I < n; ++i) the {111 intx =get_i (); the inty =get_i ();113 Addedge (x, y); the++Cnt[x]; the++Cnt[y]; the }117 118root[0] = tail++;119 - for(inti =1; I <= N; ++i)121 if(Cnt[i] = =1) DFS (I,0);122 123 Long LongAns =0LL;124 the intHead =0, tail =0;126 127que[tail++] =1; - 129 while(Head! =tail) the {131 intt = que[head++]; the if(t! =1)133Ans + = step[t]-Step[fail[t]];134 for(inti =0; I < m; ++i)135 if(Next[t][i] &&!Vis[next[t][i]])136Vis[next[t][i]] =1, que[tail++] =Next[t][i];137 }138 139printf"%lld\n", ans); $}
@Author: Yousiki
Bzoj 3926: [Zjoi2015] Fantasy Township of the Gods