Cut 'em all!
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line contains an integer nn (1≤n≤1051≤n≤105) denoting the size of the tree.

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

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

Output

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.

Examples
input

Copy
4
2 4
4 1
3 1
output

Copy
1
input

Copy
3
1 2
1 3
output

Copy
-1
input

Copy
10
7 1
8 4
8 10
4 7
6 5
9 3
3 5
2 10
2 5
output

Copy
4
input

Copy
2
1 2
output

Copy
0
Note

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.

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
int hd[maxn], ne[maxn*], to[maxn*], num, n, siz[maxn], ans;
void add( int x, int y ) {
to[++num]=y, ne[num]=hd[x], hd[x]=num;
} void dfs( int x, int fa ) {
siz[x]=;
for( int i = hd[x]; i; i = ne[i] ) {
if(to[i]!=fa){
dfs(to[i],x);
siz[x]+=siz[to[i]];
}
}
if(!(siz[x]&))
siz[x]=,ans++;
} int main(){
std::ios::sync_with_stdio(false);
scanf("%d",&n);
int uu, vv;
for( int i = ; i < n; i ++ ) {
scanf("%d%d",&uu,&vv);
add(uu,vv), add(vv,uu);
}
if( n & ) {
puts("-1");
return ;
} dfs( , - ), ans--; printf("%d\n", ans );
return ;
}

CF982C Cut 'em all! DFS 树 * 二十一的更多相关文章

  1. CF982 C Cut 'em all!【树/DFS/思维】

    [链接]:CF982C [题意]:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边.若不能切,输出-1. [分析]: 1.若点数n为奇数,因为奇数不可能分为偶数, ...

  2. Codeforces --- 982C Cut 'em all! DFS加贪心

    题目链接: https://cn.vjudge.net/problem/1576783/origin 输入输出: ExamplesinputCopy42 44 13 1outputCopy1input ...

  3. CF982C Cut 'em all!

    思路: 在深搜过程中,贪心地把树划分成若干个连通分支就可以了.划分的条件是某个子树有偶数个节点.注意到在一次划分之后并不需要重新计数,因为一个数加上一个偶数并不影响这个数的奇偶性. 实现: #incl ...

  4. Cut 'em all! CodeForces - 982C(贪心dfs)

    K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...

  5. Citrix 服务器虚拟化之二十一 桌面虚拟化之部署Provisioning Services

    Citrix 服务器虚拟化之二十一  桌面虚拟化之部署Provisioning Services Provisioning Services 是Citrix 出品的一系列虚拟化产品中最核心的一个组件, ...

  6. COJ 0979 WZJ的数据结构(负二十一)

    WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...

  7. BZOJ4878 挑战NP-Hard(dfs树)

    既然是二选一,考虑两个问题有什么联系.题面没有说无解怎么办,所以如果不存在经过k条边的简单路径,一定存在k染色方案.考虑怎么证明这个东西,我们造一棵dfs树.于是可以发现如果树深>k(根节点深度 ...

  8. 《剑指Offer》题二十一~题三十

    二十一.调整数组顺序使奇数位于偶数前面 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 测试用例: 功能测试:输入数组中的奇 ...

  9. [2018冬令营模拟测试赛(二十一)]Problem A: Decalcomania

    [2018冬令营模拟测试赛(二十一)]Problem A: Decalcomania 试题描述 输入 见"试题描述" 输出 见"试题描述" 输入示例 见&quo ...

随机推荐

  1. 表单与v-model

    基本用法 vue.js提供了v-model指令,用于在表单类元素上双向绑定数据,例如在输入框上使用,输入的内容会实时映射到绑定的数据上 example1:修改input框内容,message会实时渲染 ...

  2. 【vue】------ 路由创建 ------ 【William】

    路由常用的配置项: path:路由请求的路径 component:路由匹配成功后需要渲染的组件或者页面 tag:改变组件内部渲染的元素 假设组件内部渲染的是a标签 tag="li" ...

  3. scripts may close only the windows that were opened by it 浏览器JS控制无法关闭当前页面

    非window.open形式打开的子页面用js的window.close在chrome下就会提示scripts may close only the windows that were opened ...

  4. ImageView 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  5. Caffeine Cache-高性能Java本地缓存组件

    前面刚说到Guava Cache,他的优点是封装了get,put操作:提供线程安全的缓存操作:提供过期策略:提供回收策略:缓存监控.当缓存的数据超过最大值时,使用LRU算法替换.这一篇我们将要谈到一个 ...

  6. Redis——发布和订阅

    发布与订阅(又称pub/sub),订阅者(listener)负责订阅频道(channel),发送者(publisher)负责向频道发送二进制字符串消息(binary string message).每 ...

  7. ssm执行流程

    SSM运行流程 1:服务器启动,创建springmvc的前端控制器DispatcherServlet,创建Spring容器对象. 加载spring-servlet.xml .applicationCo ...

  8. String与new String()的区别

    JVM为了提升性能和减少内存开销,避免字符串的重复创建,维护了一块特殊的内存空间——字符串实例池. String赋值的两种方式. 1.String str = "test"; 以这 ...

  9. React单页面应用使用antd的锚点跳转失效

    首先在react项目中引用antd的锚点 import {Anchor} from 'antd';const { Link } = Anchor; <Anchor> <Link hr ...

  10. Container killed by YARN for exceeding memory limits

    19/08/12 14:15:35 ERROR cluster.YarnScheduler: Lost executor 5 on worker01.hadoop.mobile.cn: Contain ...