report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Tree diameter - FlandreOJ: Flandre Online Judge

Tree diameter

Time limit: 1000 ms
Memory limit: 256 MB
Given a tree of $n$ vertices. Calculate the diameter of the given tree. The diameter of graph is the maximum distance between any pair of vertices. ### Input - The first line contains an integer $n$. - The next $n - 1$ lines, each line contains 2 integers $u, v$, there is an edge between $u$ and $v$. ### Output - Print a single integer, the diameter of the tree. ### Constraints - $1 \le n \le 10^5$. - $1 \le u, v \le n$. ### Example Input: ``` 6 1 2 2 3 3 4 4 5 5 6 ``` Output: ``` 5 ```
Introduction to tree
Tree
Leaves
Depth
Subtree size
Jogging
Tree diameter
White path
Topic
Tree
Rating 800
Solution (2) Solution