ONE___PIECE's blog

By ONE___PIECE, history, 3 years ago, In English

https://www.codechef.com/problems/EXEPDIAM

Code
  • Vote: I like it
  • -8
  • Vote: I do not like it

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ONE___PIECE (previous revision, new revision, compare).

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ONE___PIECE (previous revision, new revision, compare).

»
3 years ago, hide # |
← Rev. 4  
Vote: I like it 0 Vote: I do not like it

First find the diameter of the tree (either dfs or DP) let's call it dist, then count the number of nodes such that the maximum distance from it to any other node is dist or dist — 1 (you can do it by rotating root DP), let's call it cnt1 and cnt2. Then the answer is just cnt1 * (2 + 2 * n — cnt1) + 2 * cnt2.