博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF982C Cut 'em all! DFS 树 * 二十一
阅读量:7055 次
发布时间:2019-06-28

本文共 2220 字,大约阅读时间需要 7 分钟。

 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 (1n1051≤n≤105) denoting the size of the tree.

The next n1n−1 lines contain two integers uu, vv (1u,vn1≤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 #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout << #a << " " << a << endlusing namespace std;const int maxn = 1e5 + 10;const int mod = 1e9 + 7;typedef long long ll;int hd[maxn], ne[maxn*2], to[maxn*2], 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]=1; 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]&1)) siz[x]=0,ans++;} int main(){ std::ios::sync_with_stdio(false); scanf("%d",&n); int uu, vv; for( int i = 1; i < n; i ++ ) { scanf("%d%d",&uu,&vv); add(uu,vv), add(vv,uu); } if( n & 1 ) { puts("-1"); return 0; } dfs( 1, -1 ), ans--; printf("%d\n", ans ); return 0;}

 

转载于:https://www.cnblogs.com/l609929321/p/9249990.html

你可能感兴趣的文章
Spring+Quartz实现定时任务的配置方法
查看>>
rsyslog日志格式介绍
查看>>
SAP 设置或取消仓库不参与MRP运算
查看>>
python 基础(三)
查看>>
BeanShell脚本接口之this引用接口类型
查看>>
mysql的复制集群,及读写分离
查看>>
易付宝 大苏宁战略的重要武器
查看>>
IPSec ***原理与配置
查看>>
让群辉支持DTS音轨
查看>>
移动端dropload插件的使用
查看>>
剑指OFFER(java)-二维数组中的查找
查看>>
华云数据与锐捷网络达成战略合作 聚焦行业云
查看>>
RHEL5.2利用lvm增加linux根分区的容量
查看>>
MDT 2013排错Provider:SQL Network Interfaces,error:26
查看>>
桌面支持--不能显示中文字体,系统已调成中文 而且不能打字
查看>>
古城钟楼微博:葡萄城程序员演练技术的产物
查看>>
最常用的四种数据分析方法
查看>>
Mesos安装部署笔记
查看>>
epoll的作用和原理介绍
查看>>
服务器远程监控管理(一)-硬件篇
查看>>