A tree is an undirected graph that consists of nodes and ) edges such that there is exactly one path between each pair of nodes. A walk is a traversal in the graph, which can be expressed as a sequence of nodes. Nodes and edges can be repeated in the walk. Thus, we can say that a walk is a path that allows you to repeat nodes and edges. You are given a tree of nodes. However, each node has a restriction that allowsionte to be visited at most [ i ] times. Your task is to find the length of the longest walk that starts in node S and ends in Since the answer might be large, return it modulo 1 0 9 ° 7 . write the code in Python



Answer :

Answer:

is an undirected graph that consists of

nodes and

)

edges suchConstraints

2

<

1

0

5

1

2

1

[

]

1

0

5

1

edges

[

]

[

]

Sample testcases explanation

that there is exactly one path between each pair of nodes. A walk is a traversal

in the graph, which can be expressed as a sequence of nodes. Nodes and

edges can be repeated in the walk. Thus, we can say that a walk is a path that

allows you to repeat nodes and edges.

You are given a tree of

nodes. However, each node has a restriction that

allowsionte

to be visited at most

[

i

]

times.

Your task is to find the length of the longest walk that starts in node S and

ends in

Since the answer might be large, return it modulo

1

0

9

°

+

7

.

Input Format

The first line contains an integer,

,

denoting the number of nodes.

The next dine contains an integer, S

,

denoting the node that you should start

and end the walk in

Each line

of the

subsequent lines

(

where

1

)

contains an integer

describing Ais

Each line iof the

-

1

subsequent lines

(

where

0



Please provide optimised code