# Bzoj 3926 [Zjoi20150] the gods ' favoured fantasy Township (SAM)

Source: Internet
Author: User

3926: [Zjoi20150] The gods favor the fantasy township time limit:10 Sec Memory limit:512 MB
submit:615 solved:369
[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% of the data, in addition to a two open space and three vacant space adjacent to the other open space are at most with two vacant space adjacent to the "idea" from each leaf start Dfs, build generalized suffix automata, the answer is L[i]-l[fa[i] "and." From each leaf Dfs is equivalent to making each leaf the root of the tree and then building the SAM so that the paths between the nodes are transformed from the starting point to the descendant's path.　　On the SAM, the different paths from the starting point to each point are different suffixes of the string with that point as the last character, and the length is between (L[fa[i]],l[i]], and the number is l[i]-l[fa[i]]. About Generalized suffix automata:

The traditional suffix automata is to solve the matching problem of single main string, and the generalized suffix automaton can be used to solve the matching problem of multiple main strings.

How to build multiple main strings into generalized suffix automata? First, a main string is established as a suffix automaton, so that after the last will be reset, so that Last=root, the next string to start from the beginning of the node, the next state if not present, then the rules of the prefix automaton to establish a new node.

If the next state has been established, we can move directly to that state, since arriving at that state indicates that all suffixes of the string that have been matched successfully are in that state and the parent node of that state, parent node ... Until root, so we need to be on that state as well as his parent node, the parent node of his parent node ... Until root is updated, the content of the update is of course different from the topic, if you ask for the number of occurrences of a string, cnt++, if the location of a string appears, the position exists in an array of nodes.

Quote from here

Code
`#include <cstdio> #include <vector> #include <cstring> #include <iostream>using namespace std; const int N = 4*1e6+10;vector<int> g[n];int n,m,c[n],in[n];int sz,fa[n],ch[n][10],l[n];int Add (int c,int p) {int np= ++sz; L[np]=l[p]+1;for (;p &&!ch[p][c];p =fa[p]) ch[p][c]=np;if (!p) fa[np]=1;else {int q=ch[p][c];if (l[q]==l[p]+1) fa[np]=q;else {int nq=++sz; l[nq]=l[p]+1;memcpy (ch[nq],ch[q],sizeof (Ch[q])); Fa[nq]=fa[q];fa[np]=fa[q]=nq;for (; q== CH[P][C];p =fa[p]) ch[p][c]=nq;}} return NP;} void Dfs (int u,int fa,int p) {int t=add (c[u],p); for (int i=0;i<g[u].size (); i++) {int v=g[u][i];if (V!=FA) DFS (v,u,t);}} void read (int& x) {char c=getchar (); int f=1; X=0;while (!isdigit (c)) {if (c== '-') f=-1; C=getchar ();} while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); x*=f;} int main () {//freopen ("in.in", "R", stdin),//freopen ("Out.out", "w", stdout); Read (n), read (m); for (int i=1;i<=n;i++) Read (c[i]); int u,v;for (int i=0;i<n-1;i++) {read (U), read (v); G[u].push_back (v); G[v].push_back (u); in[v]++, in[u]++;} sz=1;for (int i=1;i<=n;i++) if (in[i]==1) DFS (i,-1,1); long long ans=0;for (int i=1;i<=sz;i++) ans+=l[i]-l[fa[i]]; printf ("%lld", ans); return 0;}`

Bzoj 3926 [Zjoi20150] the gods ' favoured fantasy Township (SAM)

Related Keywords:

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.