题意大概就是问你最多可以割多少条边(在一棵树上),使得剩余的所有联通块的size都是偶数。。。

显然,如果n是奇数的话,一刀都切不了啊,因为奇数必定=奇数+偶数。

n是偶数的话,那就随便dfs贪心一下,每次遍历完一个点的时候(一定是深度大的优先),如果size已经是偶数,那么直接割掉。

虽然不是很会证明这种贪心,但是根据我学OI多年的经验这个贪心应该是很靠谱的23333

(为什么现在写CF题解都会吞啊,,,只能放前面了QWQ)

Discription

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
42 44 13 1

Output
1

Input
31 21 3

Output
-1

Input
107 18 48 104 76 59 33 52 102 5

Output
4

Input
21 2

Output
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<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100005;
int hd[maxn],ne[maxn*2],to[maxn*2],num,n,siz[maxn],ans;
inline 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(){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/JYYHH/p/9055195.html

CodeForces - 982C Cut 'em all!相关推荐

  1. Codeforces 982 C. Cut 'em all! 图的遍历

    点击打开链接 C. Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. Codeforces 982 C. Cut 'em all!(dfs)

    解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...

  3. Codeforces Round #484 (Div. 2) C. Cut 'em all!

    Example 1 input 4 2 4 4 1 3 1 output 1Example 2 input 3 1 2 1 3 output -1Example 3 input 10 7 1 8 4 ...

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

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

  5. CodeForces - 1514D Cut and Stick(线段树/随机数)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的数列,需要回答 mmm 次询问,每次询问给出一段区间 [l,r][l,r][l,r],输出至少需要将这段区间拆成的最少段数,使得每段区间中,假设 ...

  6. CodeForces - 1516D Cut(思维+倍增)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的数列 aaa ,现在需要回答 mmm 次询问,每次询问给出一段区间 [l,r][l,r][l,r],需要回答最少需要将区间切分成几段,才能使得每 ...

  7. [读码时间] 自定义右键菜单

    说明:代码取自网络,CSS中注释为笔者学习时添加,JS中的注释为原文所有! <!DOCTYPE html> <html> <head><meta charse ...

  8. Codeforces Round #717 (Div. 2) D. Cut 倍增

    传送门 文章目录 题意: 思路: 题意: 给定长度为nnn的序列,有qqq个询问,每次询问一个区间,输出至少将这个区间分成多少个连续区间才能使每个区间内的数互质. 思路: 首先要判断互质,这个比较容易 ...

  9. Codeforces Round #716 (Div. 2) D. Cut and Stick 主席树 + 思维

    传送门 文章目录 题意: 思路: 题意: 给你个长为nnn的数组aaa,定义好的区间为这个区间中每个数出现的次数≤⌈n2⌉\le \left \lceil \frac{n}{2} \right \rc ...

  10. Codeforces Gym101246C:Explode 'Em All(DP + bitset)

    http://codeforces.com/gym/101246/problem/C 题意:给出一个n*m的图,"*"表示这个地方需要炸掉,炸弹可以如果丢在(i,j)位置的话,那么 ...

最新文章

  1. 中国AI产业背后的富士康工人:拿低工资为硅谷巨头的数据贴标
  2. mysqlsql怎么比较当前月与去年的这个月的同比_多数房企前10月业绩稳步增长 这12家企业为何“负增长”?...
  3. 2021衢二中高考成绩查询入口,2021衢州市地区高考成绩排名查询,衢州市高考各高中成绩喜报榜单...
  4. 中国人工智能学会通讯——KS-Studio:一个知识计算引擎 1.2 知识图谱构建
  5. 【NOIP2015day1T2】【codevs4511】信息传递,最小环
  6. openssh 虚拟机linux_Alpine Linux虚拟机安装过程
  7. ROS笔记之使用Python代码实现rosbag info获取bag的信息
  8. 目标检测(十)--SSD
  9. 【2023年战略管理公开课计划】向华为学习 业务领先的战略规划SP(BLM)和战略解码BP(BEM)
  10. 欧派caxa设计软件_CAXA软件三维设计的基本使用方法
  11. 下载youtube 在线工具_5款不用下载的免费在线做图工具,满足你日常图片处理需求...
  12. linux v4l2-ctl,V4L2总结
  13. html 圣杯布局 高度,web圣杯布局
  14. Vue进阶(六十三):如何使浏览器打开时,默认的文档模式就是标准模式
  15. 台湾大学林轩田教授机器学习基石课程理解及python实现----PLA
  16. uniapp 调用安卓原生插件 安卓原生又调用了第三方sdk(第三方原生开发的aar怎么转成uni可以使用的aar)
  17. 证书透明度Certificate Transparency
  18. 这一周,我肝了公司的聚合代扣支付网关!
  19. 使用u盘PE安装原版xp系统
  20. 数电实验:自动生成三位JK卡诺图

热门文章

  1. 【水】弱化版魔术球问题
  2. IE7 绝对定位z-index问题
  3. 豆瓣评分9.2,GitHub 3.3k的学霸笔记终于出书了!获得李宏毅等大佬好评 -- 文末送书...
  4. Kaggle数据增强攻略来了!不氪金实现50种语言互译
  5. EMNLP 2021 | 罗氏和博阿齐奇大学研究合作团队提出:多标签文本分类中长尾分布的平衡策略...
  6. 【收藏】这些Python代码技巧,你肯定还不知道
  7. [ACL18]直接到树:基于神经句法距离的成分句法分析
  8. 神奇的G1——Java全新垃圾回收机制
  9. 早期预警系统的组成要素
  10. 写一本Linux内核方面的书籍