Longest Paths in Tree
Practice
4.8 (10 votes)
Algorithms
Approved
Graphs
Medium
Problem
85% Success 462 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree. Simple path of length m is a sequence of vertices \(v_1, v_2, \dots, v_m\) such that

  • All \(v_i\) are distinct.
  • \(v_i\) and \(v_{i+1}\) are connected by edge for \(1 \leqslant i \leqslant m - 1\).

For each vertex find length of longest simple path that goes through this vertex. Also count number of this paths. Two paths considered distinct if set of vertices of this paths differ.

Input Format:
First line of input contains single positive integer n -- number of vertices of the tree.
Next \(n - 1\) lines contains pairs space-separated integers \(u_i, v_i\) -- edges of the tree.

Output Format:
Output n lines. i-th line must contain two integers -- length of the longest simple path containing vertex i and number of such paths.

Constraints:
\(1 \leqslant n \leqslant 300,000.\)

Scoring:
50 points
Additionally \(n \leqslant 3,000\).
50 points
No additional constraints.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium
Points:30
152 votes
Tags:
ApprovedDepth First SearchGraphsMediumOpen
Points:30
4 votes
Tags:
GraphsAlgorithmsDepth First Search