CF1458F.Range Diameter Sum

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn vertices numbered 1,,n1, \ldots, n . A tree is a connected simple graph without cycles.

Let dist(u,v)\mathrm{dist}(u, v) be the number of edges in the unique simple path connecting vertices uu and vv .

Let diam(l,r)=maxdist(u,v)\mathrm{diam}(l, r) = \max \mathrm{dist}(u, v) over all pairs u,vu, v such that lu,vrl \leq u, v \leq r .

Compute 1lrndiam(l,r)\sum_{1 \leq l \leq r \leq n} \mathrm{diam}(l, r) .

输入格式

The first line contains a single integer nn ( 1n1051 \leq n \leq 10^5 ) — the number of vertices in the tree.

The next n1n - 1 lines describe the tree edges. Each of these lines contains two integers u,vu, v ( 1u,vn1 \leq u, v \leq n ) — endpoint indices of the respective tree edge. It is guaranteed that the edge list indeed describes a tree.

输出格式

Print a single integer — 1lrndiam(l,r)\sum_{1 \leq l \leq r \leq n} \mathrm{diam}(l, r) .

输入输出样例

  • 输入#1

    4
    1 2
    2 4
    3 2

    输出#1

    10
  • 输入#2

    10
    1 8
    2 9
    5 6
    4 8
    4 2
    7 9
    3 6
    10 4
    3 9

    输出#2

    224
首页