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 ;
}

最新文章

  1. Ret2Libc 练习(2) -- VirtualProtect
  2. Unity5.x版本AssetBundle打包研究
  3. hadoop运维经验
  4. RMAN_学习笔记2_RMAN Setup配置和监控
  5. recyclview多条目布局
  6. bower install和cnpm install
  7. 第五节:AppDomain FirstChance异常通知
  8. hdu 2594-Simpsons’ Hidden Talents(KMP)
  9. 学习Python编程的11个资源
  10. Ubuntu 安装 Sun JDK
  11. (原)linux 编译 lwqq
  12. DIV焦点事件
  13. [转]chrome技术文档列表
  14. re2c实例
  15. ggplot2 geom相关设置——添加线条
  16. linux中将程序加入到开机自动启动
  17. Linux入门(15)——Ubuntu16.04安装codeblocks搭建C/C++的IDE环境
  18. PHP MySQL Delete
  19. C语言的main函数到底该怎么写
  20. (转载)Sublime Text 3 快捷键大全

热门文章

  1. MySQL20个经典面试题
  2. 在Linux下设置定时任务(每分钟执行一次特定的shell脚本)
  3. 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
  4. widow系统 LuaForWindows,安装 luasocket
  5. ThreadDeath 理解
  6. erlang游戏开发tcp
  7. test20190308
  8. Google的跨平台开发高质量原生 UI 的移动 SDK---Flutter免费且开源
  9. CALayer1-简介
  10. Django之Models