A11850 | Cut 'em all!
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You're given a tree with $n$ vertices.
Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.
给你一棵有 $n$ 个顶点的树。
你的任务是确定可以移除的最大边数,从而使所有剩余的连接部分大小相同。
Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.
给你一棵有 $n$ 个顶点的树。
你的任务是确定可以移除的最大边数,从而使所有剩余的连接部分大小相同。
输入格式
The first line contains an integer $n$ ( $1 \le n \le 10^5$ ) denoting the size of the tree.
The next $n - 1$ lines contain two integers $u$ , $v$ ( $1 \le u, v \le n$ ) each, describing the vertices connected by the $i$ -th edge.
It's guaranteed that the given edges form a tree.
输入
第一行包含一个整数 $n$ ( $1 \le n \le 10^5$ ),表示树的大小。
接下来的 $n - 1$ 行分别包含两个整数 $u$ 、 $v$ ( $1 \le u, v \le n$ ),描述由 $i$ -th 边连接的顶点。
可以保证给定的边构成一棵树。
The next $n - 1$ lines contain two integers $u$ , $v$ ( $1 \le u, v \le n$ ) each, describing the vertices connected by the $i$ -th edge.
It's guaranteed that the given edges form a tree.
输入
第一行包含一个整数 $n$ ( $1 \le n \le 10^5$ ),表示树的大小。
接下来的 $n - 1$ 行分别包含两个整数 $u$ 、 $v$ ( $1 \le u, v \le n$ ),描述由 $i$ -th 边连接的顶点。
可以保证给定的边构成一棵树。
输出格式
Output a single integer $k$ — the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property.
输出
输出单个整数 $k$ - 为使所有相连的组件大小相同而可以去除的最大边数,或者 $-1$ - 如果无法去除边以满足此属性。
输出
输出单个整数 $k$ - 为使所有相连的组件大小相同而可以去除的最大边数,或者 $-1$ - 如果无法去除边以满足此属性。
输入输出样例
输入 #1
4 2 4 4 1 3 1
输出 #1
1
输入 #2
3 1 2 1 3
输出 #2
-1
输入 #3
10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5
输出 #3
4
输入 #4
2 1 2
输出 #4
0
In the first example you can remove the edge between vertices $1$ and $4$ . The graph after that will have two connected components with two vertices in each.
In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$ .
备注
在第一个示例中,您可以删除顶点 $1$ 和 $4$ 之间的边。之后的图形将有两个相连的部分,每个部分有两个顶点。
在第二个示例中,无法通过删除边的方式使所有分量的顶点数都是偶数,因此答案是 $-1$ 。
In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$ .
备注
在第一个示例中,您可以删除顶点 $1$ 和 $4$ 之间的边。之后的图形将有两个相连的部分,每个部分有两个顶点。
在第二个示例中,无法通过删除边的方式使所有分量的顶点数都是偶数,因此答案是 $-1$ 。
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted