CF982C.Cut 'em all!

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You're given a tree with nn 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.

给你一棵包含 nn 个顶点的树。

你的任务是确定最多可以删除多少条边,使得剩余的所有连通分量的大小均为偶数。

输入格式

The first line contains an integer nn (1≤n≤1051 \le n \le 10^5) denoting the size of the tree.

The next n−1n - 1 lines contain two integers uu, vv (1≤u,v≤n1 \le u, v \le n) each, describing the vertices connected by the ii-th edge.

It's guaranteed that the given edges form a tree.

第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示树的大小。

接下来的 n−1n - 1 行每行包含两个整数 uu、vv(1≤u,v≤n1 \le u, v \le n),描述第 ii 条边所连接的两个顶点。

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

输出格式

Output a single integer kk — the maximum number of edges that can be removed to leave all connected components with even size, or −1-1 if it is impossible to remove edges in order to satisfy this property.

输出一个整数 kk —— 满足删除后所有连通分量的大小均为偶数的最大可删除边数;若无法通过删边使所有连通分量的大小均为偶数,则输出 −1-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 11 and 44. 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.

在第一个例子中,你可以删除顶点 11 和 44 之间的边。删除后,图将分为两个连通分量,每个分量包含两个顶点。

在第二个例子中,无法通过删除边使得所有连通分量均包含偶数个顶点,因此答案为 −1-1。

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

首页