Блог пользователя ONE___PIECE

Автор ONE___PIECE, история, 3 года назад, По-английски

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

Code
  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, скрыть # |
← Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

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.