C. 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

二、大致题意

给出n个点,有n-1条边让他们相连。

询问最多删除多少边,让他们成为偶数的块

三、思路

  DFS,只要能找到一个偶数的块,就使答案++,因为是偶数块的话,就可以在这里断开一次

(其实感觉一点都不裸,太菜,没想到dfs)

#include <iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#define ll unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
bool used[];
int n;
vector<int>v[];
int ans=;
int dfs(int fa)
{
used[fa]=;
int s=;
for(int i=;i<v[fa].size ();i++)
{
if(used[v[fa][i]]) continue;
s=s+dfs(v[fa][i]);
}
if((s+)%==) ans++;//发现是偶数块,就可以减一刀
return s+;
}
int main()
{
int n;
cin>>n;
ans=;
memset(used,,sizeof(used));
for(int i=;i<=n-;i++)
{
int x,y;
cin>>x>>y;
v[x].push_back (y);
v[y].push_back (x);
}
if(n&) cout<<"-1";
else
{
used[]=;
int s=dfs();
cout<<ans-;//原本自己就是偶数,所以要减1
}
return ;
}

Codeforces 982C(dfs+思维)的更多相关文章

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

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

  2. hdu6035[dfs+思维] 2017多校1

    /*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...

  3. D. Eternal Victory(dfs + 思维)

    D. Eternal Victory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  5. AND Graph CodeForces - 987F(思维二进制dfs)

    题意:给出n(0≤n≤22)和m,和m个数ai,1 ≤ m ≤ 2n ,0≤ai<2n ,把ai & aj == 0 的连边,求最后有几个连通块 解析:一个一个去找肯定爆,那么就要转换一 ...

  6. Codeforces 931D Peculiar apple-tree(dfs+思维)

    题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...

  7. Codeforces Gym101246G:Revolutionary Roads(DFS+思维)

    http://codeforces.com/gym/101246/problem/G 题意:有一个n个点m条边的有向图,现在可以修改某一条有向边使得其为无向边,问修改哪些边可以使得修改后的强连通分量的 ...

  8. Codeforces 760C:Pavel and barbecue(DFS+思维)

    http://codeforces.com/problemset/problem/760/C 题意:有n个盘子,每个盘子有一块肉,当肉路过这个盘子的时候,当前朝下的这一面会被煎熟,每个盘子有两个数,p ...

  9. codeforces 799 D. Field expansion(dfs+思维剪枝)

    题目链接:http://codeforces.com/contest/799/problem/D 题意:给出h*w的矩阵,要求经过操作使得h*w的矩阵能够放下a*b的矩阵,操作为:将长或者宽*z[i] ...

随机推荐

  1. Python面向对象的三大特征 --- 封装、继承、多态

    一.封装 1)封装:是面向对象的一大特点:将属性和方法封装在一个抽象类中.外界使用类创建对象,然后让对象调用内部方法.对象方法的细节都被封装在类的内部. class Person():   def _ ...

  2. vscode 实用插件

    1.Auto Rename Tag,非常实用!要修改标签名称的时候自动修改结束标签,节省一半时间,提升效率,非常棒! 2.vscode-icon,这款必须要推荐,明显提升效率的小插件,在项目文件多类型 ...

  3. 用css和php脚本来使得poscms的list标签顺利输出记录

    每次拿到前端给的页面,都会有意想不到的惊喜,因为他们给的页面总是不能很好地契合poscms的模板标签 输出规范,这时候就需要动点脑筋去解决问题了. 拿前两天拿到的一类(对,你没看错,是一类)页面来说吧 ...

  4. Java泛型常见面试题

    怀着崇拜的心情读完这篇文章,却发现作者不是原创,而不标注转载~所以转载地址不详~ 1. Java中的泛型是什么 ? 使用泛型的好处是什么? 这是在各种Java泛型面试中,一开场你就会被问到的问题中的一 ...

  5. 随想录(kgdb的基本原理)

    [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 在linux kernel调试的时候,有一种很方便的调试方法,那就是kgdb.kgdb的本质,就 ...

  6. 基于Vue的后台选择推荐

    引言: Vue.js目前是业界大名鼎鼎的Web解决方案,具体有点,我这里就不再赘述了,感兴趣的童鞋自行查找阅读,这里罗列一下,这几天自己研究的成果,管理后台. 管理后台 Vue Element Adm ...

  7. js `` 手机不支持

  8. linux命令-xz

    tar.xz文件如何压缩解压xz是绝大数linux默认就带的一个压缩工具,压缩率很高. xz压缩文件方法 默认压缩等级是6.要设置压缩率加入参数 -0 到 -9调节压缩率. xz -z [文件名] 不 ...

  9. vim让一些不可见的字符显示出来吧

    http://www.cnblogs.com/chenwenbiao/archive/2011/10/26/2225467.html :set list

  10. Mac安装最新tensorflow遇到的坑,记录下方便后人

    之前其他mac电脑安装tensorflow时候一切顺利,一行命令sudo pip install tensorflow就高搞定了,但是今天在新mac上安装tensorflow时候出现了一个bug,搞了 ...