A13641 | Tree Modification
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree with $n$ vertices. You are allowed to modify the structure of the tree through the following multi-step operation:
1. Choose three vertices $a$ , $b$ , and $c$ such that $b$ is adjacent to both $a$ and $c$ .
2. For every vertex $d$ other than $b$ that is adjacent to $a$ , remove the edge connecting $d$ and $a$ and add the edge connecting $d$ and $c$ .
3. Delete the edge connecting $a$ and $b$ and add the edge connecting $a$ and $c$ .
As an example, consider the following tree:
The following diagram illustrates the sequence of steps that happen when we apply an operation to vertices $2$ , $4$ , and $5$ :
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 - 1$ , called its center, and $n - 1$ vertices of degree $1$ .
1. Choose three vertices $a$ , $b$ , and $c$ such that $b$ is adjacent to both $a$ and $c$ .
2. For every vertex $d$ other than $b$ that is adjacent to $a$ , remove the edge connecting $d$ and $a$ and add the edge connecting $d$ and $c$ .
3. Delete the edge connecting $a$ and $b$ and add the edge connecting $a$ and $c$ .
As an example, consider the following tree:
The following diagram illustrates the sequence of steps that happen when we apply an operation to vertices $2$ , $4$ , and $5$ :
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 - 1$ , called its center, and $n - 1$ vertices of degree $1$ .
输入格式
The first line contains an integer $n$ ( $3 \le n \le 2 \cdot 10^5$ ) — the number of vertices in the tree.
The $i$ -th of the following $n - 1$ lines contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ , $u_i \neq v_i$ ) denoting that there exists an edge connecting vertices $u_i$ and $v_i$ . It is guaranteed that the given edges form a tree.
The $i$ -th of the following $n - 1$ lines contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ , $u_i \neq v_i$ ) denoting that there exists an edge connecting vertices $u_i$ and $v_i$ . It is guaranteed that the given edges form a tree.
输出格式
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 $10^{18}$ operations.
It can be proven that under the given constraints, it is always possible to transform the tree into a star using at most $10^{18}$ operations.
输入输出样例
输入 #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 $5$ by applying a single operation to vertices $2$ , $4$ , and $5$ .
In the second test case, the given tree is already a star with the center at vertex $4$ , so no operations have to be performed.
In the second test case, the given tree is already a star with the center at vertex $4$ , so no operations have to be performed.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted