题库练习 Cut 'em all!
← 上一题 下一题 →

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$ 个顶点的树。

你的任务是确定可以移除的最大边数,从而使所有剩余的连接部分大小相同。

输入格式

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 边连接的顶点。

可以保证给定的边构成一棵树。

输出格式

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$ - 如果无法去除边以满足此属性。

输入输出样例

输入 #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
C++ 编辑器
输入
输出