测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A11850. Cut 'em all!

编程题 普及/提高-

题目描述

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

说明/提示

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$ 。
上一题 去做题 下一题