CF982C Cut 'em all! DFS 树 * 二十一
1 second
256 megabytes
standard input
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.
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 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.
4
2 4
4 1
3 1
1
3
1 2
1 3
-1
10
7 1
8 4
8 10
4 7
6 5
9 3
3 5
2 10
2 5
4
2
1 2
0
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 树 * 二十一的更多相关文章
- CF982 C Cut 'em all!【树/DFS/思维】
[链接]:CF982C [题意]:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边.若不能切,输出-1. [分析]: 1.若点数n为奇数,因为奇数不可能分为偶数, ...
- Codeforces --- 982C Cut 'em all! DFS加贪心
题目链接: https://cn.vjudge.net/problem/1576783/origin 输入输出: ExamplesinputCopy42 44 13 1outputCopy1input ...
- CF982C Cut 'em all!
思路: 在深搜过程中,贪心地把树划分成若干个连通分支就可以了.划分的条件是某个子树有偶数个节点.注意到在一次划分之后并不需要重新计数,因为一个数加上一个偶数并不影响这个数的奇偶性. 实现: #incl ...
- Cut 'em all! CodeForces - 982C(贪心dfs)
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...
- Citrix 服务器虚拟化之二十一 桌面虚拟化之部署Provisioning Services
Citrix 服务器虚拟化之二十一 桌面虚拟化之部署Provisioning Services Provisioning Services 是Citrix 出品的一系列虚拟化产品中最核心的一个组件, ...
- COJ 0979 WZJ的数据结构(负二十一)
WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...
- BZOJ4878 挑战NP-Hard(dfs树)
既然是二选一,考虑两个问题有什么联系.题面没有说无解怎么办,所以如果不存在经过k条边的简单路径,一定存在k染色方案.考虑怎么证明这个东西,我们造一棵dfs树.于是可以发现如果树深>k(根节点深度 ...
- 《剑指Offer》题二十一~题三十
二十一.调整数组顺序使奇数位于偶数前面 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 测试用例: 功能测试:输入数组中的奇 ...
- [2018冬令营模拟测试赛(二十一)]Problem A: Decalcomania
[2018冬令营模拟测试赛(二十一)]Problem A: Decalcomania 试题描述 输入 见"试题描述" 输出 见"试题描述" 输入示例 见&quo ...
随机推荐
- wamp不显示文件图标
wamp不显示文件图标 效果如下图 右键图片"在新的标签页打开图片"后会跳转到404页面,并显示The requested URL /icons/unknown.gif was n ...
- Windows的 IIS 部署django项目
Windows的 IIS 部署django项目 1.安装Windows的IIS 功能(win10为例): (1)进入控制面板 :选择大图标 进入程序和功能 (2)启用或者关闭Windows功能 ...
- 7.源码分析---SOFARPC是如何实现故障剔除的?
我在服务端引用那篇文章里面分析到,服务端在引用的时候会去获取服务端可用的服务,并进行心跳,维护一个可用的集合. 所以我们从客户端初始化这部分说起. 服务连接的维护 客户端初始化的时候会调用cluste ...
- Scala类和对象(二)
1. 类和属性 1.1 如何控制构造函数字段的可见性 在Scala中: 如果一个字段被声明为var, Scala会为该字段生成getter和setter方法. 如果字段是val, Scala只生成ge ...
- ccf 201903-5 317号子任务(60分)
看到这题,第一印象,用dijkstra算法求n次单源最短路,时间复杂度O(n^3),超时30分妥妥的. 于是用优先队列优化,O(n*mlogm),快很多,但依然30. 那么不妨换一种思路,题目要求的是 ...
- DIY cnblog——背景渐变切换
进来的小伙伴应该已经看过了我的博客样式,但还是贴张图先: 先大致说一下实现的思路,然后把代码贴出来供小伙伴们参考. 由于不是特别技术性的文章,格式就放宽松一点,跟着意识流走吧. 先跟大家分享一个渐变背 ...
- h5微信分享
h5分享的步骤(前端需要完成的部分) 1.绑定域名 登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. 2.引入Js文件 在需要调用JS接口的页面引入如下JS文件,(支持ht ...
- 存在于文件名中的SQL手工注入
SQL注入已经在前一章为大家介绍了个大概,本文将讲述我遇到的本以为是文件上传漏洞,却是以文件名触发的SQL注入! 本文分享的内容同样来自于一道CTF题! 1. 直接进入正题 (1) 初步探测 先看一下 ...
- sql server数据库查询链接服务器
服务器对象->链接服务器: 或者 select * from sys.servers: 找到服务器对象名称 select * from [服务器对象名称].[数据库名称].dbo.[表名]:
- 8.7 day28 网络编程 socket套接字 半连接池 通信循环 粘包问题 struct模块
前置知识:不同计算机程序之间的数据传输 应用程序中的数据都是从程序所在计算机内存中读取的. 内存中的数据是从硬盘读取或者网络传输过来的 不同计算机程序数据传输需要经过七层协议物理连接介质才能到达目标程 ...