import
java.util.*;
class
GFG {
static
int
ans;
static
void
dfs(
int
src,
int
StartValue,
ArrayList<ArrayList<Integer> > edge,
int
A[],
int
parent)
{
for
(Integer it : edge.get(src)) {
if
(it != parent) {
int
CurrentValue = A[it -
1
];
if
(CurrentValue > StartValue)
continue
;
if
(CurrentValue == StartValue)
ans++;
dfs(it, StartValue, edge, A, src);
}
}
return
;
}
static
int
CountSpecialPath(
int
N,
int
[][] edges,
int
A[])
{
ArrayList<ArrayList<Integer> > edge
=
new
ArrayList<>();
for
(
int
i =
0
; i <= N; i++) {
edge.add(
new
ArrayList<>());
}
for
(
int
i =
0
; i < N -
1
; i++) {
int
x = edges[i][
0
];
int
y = edges[i][
1
];
edge.get(x).add(y);
edge.get(y).add(x);
}
ans =
0
;
for
(
int
i =
1
; i <= N; i++) {
int
StartValue = A[i -
1
];
dfs(i, StartValue, edge, A, -
1
);
}
ans /=
2
;
return
ans;
}
public
static
void
main(String[] args)
{
int
N =
5
;
int
[][] edges
= { {
1
,
2
}, {
1
,
3
}, {
3
,
4
}, {
4
,
5
} };
int
A[] = {
2
,
3
,
1
,
2
,
3
};
System.out.println(CountSpecialPath(N, edges, A));
}
}