2867. Count Valid Paths in a Tree
There is an undirected tree with
n
nodes labeled from1
ton
. You are given the integern
and a 2D integer arrayedges
of lengthn - 1
, whereedges[i] = [ui, vi]
indicates that there is an edge between nodesui
andvi
in the tree.Return the number 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 froma
tob
.Note that:
- The path
(a, b)
is a sequence of distinct nodes starting with nodea
and ending with nodeb
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.
c++
1 |
|