CF1375G.Tree Modification

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree with nn vertices. You are allowed to modify the structure of the tree through the following multi-step operation:

  1. Choose three vertices aa, bb, and cc such that bb is adjacent to both aa and cc.
  2. For every vertex dd other than bb that is adjacent to aa, remove the edge connecting dd and aa and add the edge connecting dd and cc.
  3. Delete the edge connecting aa and bb and add the edge connecting aa and cc.

As an example, consider the following tree:

The following diagram illustrates the sequence of steps that happen when we apply an operation to vertices 22, 44, and 55:

It can be proven that after each operation, the resulting graph is still a tree.

Find the minimum number of operations that must be performed to transform the tree into a star. A star is a tree with one vertex of degree n−1n - 1, called its center, and n−1n - 1 vertices of degree 11.

给你一棵包含 nn 个顶点的树。你可以通过以下多步操作来修改该树的结构:

  1. 选择三个顶点 aa、bb 和 cc,使得 bb 同时与 aa 和 cc 相邻。
  2. 对于每一个除 bb 外与 aa 相邻的顶点 dd,删除边 (d,a)(d, a),并添加边 (d,c)(d, c)。
  3. 删除边 (a,b)(a, b),并添加边 (a,c)(a, c)。

例如,考虑如下这棵树:

下图展示了对顶点 22、44、55 执行一次操作时所发生的步骤序列:

可以证明:每次操作后所得图仍为一棵树。

求将该树变为星形图所需的最少操作次数。星形图是一种树,其中存在一个度数为 n−1n - 1 的顶点(称为中心),其余 n−1n - 1 个顶点的度数均为 11。

输入格式

The first line contains an integer nn (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.

The ii-th of the following n−1n - 1 lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \neq v_i) denoting that there exists an edge connecting vertices uiu_i and viv_i. It is guaranteed that the given edges form a tree.

第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)——树中顶点的数量。

接下来的 n−1n - 1 行中,第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n,ui≠viu_i \neq v_i),表示顶点 uiu_i 与 viv_i 之间存在一条边。保证所给的边构成一棵树。

输出格式

Print a single integer — the minimum number of operations needed to transform the tree into a star.

It can be proven that under the given constraints, it is always possible to transform the tree into a star using at most 101810^{18} operations.

输出一个整数——将树转化为星形图所需的最少操作次数。

在给定约束条件下,可以证明总能通过至多 101810^{18} 次操作将树转化为星形图。

输入输出样例

  • 输入#1

    6
    4 5
    2 6
    3 2
    1 2
    2 4

    输出#1

    1
  • 输入#2

    4
    2 4
    4 1
    3 4

    输出#2

    0

说明/提示

The first test case corresponds to the tree shown in the statement. As we have seen before, we can transform the tree into a star with center at vertex 55 by applying a single operation to vertices 22, 44, and 55.

In the second test case, the given tree is already a star with the center at vertex 44, so no operations have to be performed.

第一个测试用例对应题目描述中所示的树。如前所述,我们可以通过对顶点 22、44 和 55 执行一次操作,将该树转化为以顶点 55 为中心的星形树。

第二个测试用例中,给定的树本身已是中心为顶点 44 的星形树,因此无需执行任何操作。

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

首页