#K66237. Infection Spread in a Tree

    ID: 32376 Type: Default 1000ms 256MiB

Infection Spread in a Tree

Infection Spread in a Tree

Given a tree with (N) nodes and (N-1) edges, you are required to determine the number of days it will take to infect the entire tree starting from a given node (S). The infection spreads from an infected node to all its adjacent uninfected nodes in one day. In other words, you need to find the maximum distance from (S) to any other node in the tree. Mathematically, if (d(s, v)) represents the distance from the starting node (s) to node (v), then the answer is given by: [ d_{max} = \max_{v \in V} d(s,v)] ] where (V) is the set of all nodes in the tree. This is a classic breadth-first search (BFS) problem on trees.

inputFormat

Input is read from standard input (stdin). The first line contains an integer (N), the number of nodes in the tree. The next (N-1) lines each contain two integers (u) and (v), representing an undirected edge between nodes (u) and (v). The final line contains a single integer (S), indicating the starting node of the infection.

outputFormat

Output a single integer to standard output (stdout) representing the number of days required to infect the entire tree.## sample

5
1 2
1 3
3 4
3 5
3
2

</p>