There is an undirected tree with n nodes labeled from 1 to n. You are given the integer n and a 2D integer array edges of length n - 1, where
edges[i] = [ui, vi] indicates that there is an edge between nodes ui and
vi in the tree.
Return thenumber of valid paths in the tree.
A path (a, b) is valid if there exists exactly one prime number among the node labels in the path from a to b.
Note that:
The path (a, b) is a sequence of distinct nodes starting with node a and ending with node b such that every two adjacent nodes in the sequence share an edge in the tree.
Path (a, b) and path (b, a) are considered the same and counted only once.

Input: n =5, edges =[[1,2],[1,3],[2,4],[2,5]]Output: 4Explanation: The pairs with exactly one prime number on the path between them are:-(1,2) since the path from 1 to 2 contains prime number 2.-(1,3) since the path from 1 to 3 contains prime number 3.-(1,4) since the path from 1 to 4 contains prime number 2.-(2,4) since the path from 2 to 4 contains prime number 2.It can be shown that there are only 4 valid paths.

Input: n =6, edges =[[1,2],[1,3],[2,4],[3,5],[3,6]]Output: 6Explanation: The pairs with exactly one prime number on the path between them are:-(1,2) since the path from 1 to 2 contains prime number 2.-(1,3) since the path from 1 to 3 contains prime number 3.-(1,4) since the path from 1 to 4 contains prime number 2.-(1,6) since the path from 1 to 6 contains prime number 3.-(2,4) since the path from 2 to 4 contains prime number 2.-(3,6) since the path from 3 to 6 contains prime number 3.It can be shown that there are only 6 valid paths.
A valid path must contain exactly one prime-labeled node. For each prime node, count the number of nodes in its subtrees that are not prime, and use this to count valid paths passing through the prime. The answer is the sum over all primes of the number of pairs between the prime and each non-prime node in its connected components.
classSolution {
funcountPaths(n: Int, edges: Array<IntArray>): Long {
val isPrime = BooleanArray(n+1) { true }
isPrime[0] = false; isPrime[1] = falsefor (i in2..n) {
if (isPrime[i]) {
var j = i * i
while (j <= n) {
isPrime[j] = false j += i
}
}
}
val g = Array(n+1) { mutableListOf<Int>() }
for (e in edges) {
g[e[0]].add(e[1])
g[e[1]].add(e[0])
}
var ans = 0Lval vis = BooleanArray(n+1)
fundfs(u: Int): Int {
vis[u] = truevar cnt = 1for (v in g[u]) {
if (!vis[v] &&!isPrime[v]) cnt += dfs(v)
}
return cnt
}
for (i in1..n) {
if (!isPrime[i]) continue vis.fill(false)
vis[i] = truefor (v in g[i]) {
if (!isPrime[v] && !vis[v]) {
val cnt = dfs(v)
ans += cnt
}
}
}
return ans
}
}
classSolution:
defcountPaths(self, n: int, edges: list[list[int]]) -> int:
defsieve(n: int) -> list[bool]:
is_prime = [False, False] + [True] * (n-1)
for i in range(2, int(n**0.5)+1):
if is_prime[i]:
for j in range(i*i, n+1, i):
is_prime[j] =Falsereturn is_prime
is_prime = sieve(n)
g = [[] for _ in range(n+1)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
ans =0 vis = [False] * (n+1)
defdfs(u: int) -> int:
vis[u] =True cnt =1for v in g[u]:
ifnot vis[v] andnot is_prime[v]:
cnt += dfs(v)
return cnt
for i in range(1, n+1):
ifnot is_prime[i]:
continue vis = [False] * (n+1)
vis[i] =Truefor v in g[i]:
ifnot is_prime[v] andnot vis[v]:
cnt = dfs(v)
ans += cnt
return ans