CF2253E.Diameter Intersections

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given a tree with nn vertices. The diameter of a tree is a simple path of maximum length in the tree. The length of a path is the number of edges it contains. The diameter of the given tree has odd length.

We call an integer kk beautiful if it is possible to choose two diameters in this tree (possibly with the same endpoints; it is allowed to choose the same two diameters) such that their intersection contains exactly kk edges.

Find all beautiful values of kk and output them in increasing order.

给你一棵包含 nn 个顶点的树。树的直径是指树中长度最大的简单路径。路径的长度定义为该路径所包含的边数。给定树的直径长度为奇数。

我们称一个整数 kk 是“优美的”,如果可以在该树中选出两条直径(允许端点相同;也允许选择同一条直径两次),使得这两条直径的交集恰好包含 kk 条边。

请找出所有优美的 kk 值,并按升序输出。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Each test case is given in the following format:

  • the first line contains one integer nn (2≤n≤1062 \le n \le 10^6) — the number of vertices in the tree;
  • the next n−1n - 1 lines contain two integers uu and vv each (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v), denoting an edge between vertices uu and vv.

Additional constraints on the input:

  • the sum of nn over all test cases does not exceed 10610^6;
  • in each test case, the edges form a tree whose diameter has odd length.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——测试用例的数量。

每个测试用例的格式如下:

  • 第一行包含一个整数 nn(2≤n≤1062 \le n \le 10^6)——树中顶点的数量;
  • 接下来的 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \ne v),表示顶点 uu 与 vv 之间存在一条边。

输入的额外约束条件:

  • 所有测试用例中 nn 的总和不超过 10610^6;
  • 在每个测试用例中,所给边构成一棵树,且该树的直径长度为奇数。

输出格式

For each test case, print one integer mm — the number of beauitful values of kk; then print the values of kk themselves in increasing order.

对于每个测试用例,输出一个整数 mm —— 即优美的 kk 值的个数;然后按升序输出这些 kk 值本身。

输入输出样例

  • 输入#1

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

    输出#1

    1 1
    1 3
    3 1 2 3
    3 1 3 5
    4 2 3 4 5

说明/提示

Consider the first three examples:

  • in the first example, the pair of diameters (1,2)(1, 2) and (1,2)(1, 2) gives k=1k=1;
  • in the second example, the pair of diameters (1,4)(1, 4) and (4,1)(4, 1) gives k=3k=3;
  • in the third example, the pair of diameters (6,4)(6, 4) and (3,5)(3, 5) gives k=1k=1; the pair of diameters (6,4)(6, 4) and (4,5)(4, 5) gives k=2k=2; the pair of diameters (6,4)(6, 4) and (6,4)(6, 4) gives k=3k=3.

考虑前三个例子:

  • 在第一个例子中,直径对 (1,2)(1, 2) 和 (1,2)(1, 2) 给出 k=1k=1;
  • 在第二个例子中,直径对 (1,4)(1, 4) 和 (4,1)(4, 1) 给出 k=3k=3;
  • 在第三个例子中,直径对 (6,4)(6, 4) 和 (3,5)(3, 5) 给出 k=1k=1;直径对 (6,4)(6, 4) 和 (4,5)(4, 5) 给出 k=2k=2;直径对 (6,4)(6, 4) 和 (6,4)(6, 4) 给出 k=3k=3。

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

首页