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\n",sam.count());
81 }
82 return 0;
83 }