CF1458F.Range Diameter Sum

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:512MB

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)=max⁡dist(u,v)\mathrm{diam}(l, r) = \max \mathrm{dist}(u, v) over all pairs u,vu, v such that l≤u,v≤rl \leq u, v \leq r.

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

给你一棵包含 nn 个顶点的树,顶点编号为 1,…,n1, \ldots, n。树是一种无环的连通简单图。

令 dist(u,v)\mathrm{dist}(u, v) 表示连接顶点 uu 和 vv 的唯一简单路径所含的边数。

令 diam(l,r)=max⁡dist(u,v)\mathrm{diam}(l, r) = \max \mathrm{dist}(u, v),其中最大值取遍所有满足 l≤u,v≤rl \leq u, v \leq r 的顶点对 (u,v)(u, v)。

请计算 ∑1≤l≤r≤ndiam(l,r)\sum_{1 \leq l \leq r \leq n} \mathrm{diam}(l, r)。

输入格式

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

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

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)——树中顶点的数量。

接下来的 n−1n - 1 行描述树的边。每行包含两个整数 u,vu, v(1≤u,v≤n1 \leq u, v \leq n)——对应树边的两个端点编号。保证所给边列表确实构成一棵树。

输出格式

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

输出一个整数——∑1≤l≤r≤ndiam(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

输入解题思路,AI测评打分。不知道怎么写?

首页