Codeforces 982C(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
二、大致题意
给出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+思维)的更多相关文章
- Cut 'em all! CodeForces - 982C(贪心dfs)
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...
- hdu6035[dfs+思维] 2017多校1
/*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...
- D. Eternal Victory(dfs + 思维)
D. Eternal Victory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- AND Graph CodeForces - 987F(思维二进制dfs)
题意:给出n(0≤n≤22)和m,和m个数ai,1 ≤ m ≤ 2n ,0≤ai<2n ,把ai & aj == 0 的连边,求最后有几个连通块 解析:一个一个去找肯定爆,那么就要转换一 ...
- Codeforces 931D Peculiar apple-tree(dfs+思维)
题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...
- Codeforces Gym101246G:Revolutionary Roads(DFS+思维)
http://codeforces.com/gym/101246/problem/G 题意:有一个n个点m条边的有向图,现在可以修改某一条有向边使得其为无向边,问修改哪些边可以使得修改后的强连通分量的 ...
- Codeforces 760C:Pavel and barbecue(DFS+思维)
http://codeforces.com/problemset/problem/760/C 题意:有n个盘子,每个盘子有一块肉,当肉路过这个盘子的时候,当前朝下的这一面会被煎熟,每个盘子有两个数,p ...
- codeforces 799 D. Field expansion(dfs+思维剪枝)
题目链接:http://codeforces.com/contest/799/problem/D 题意:给出h*w的矩阵,要求经过操作使得h*w的矩阵能够放下a*b的矩阵,操作为:将长或者宽*z[i] ...
随机推荐
- 【css】响应式布局入门【转】
最近研究响应式设计框架的时候,发现网上很多相关的属性介绍,却很少有系统的入门级使用的文章,我自己整理了一篇入门知识,并没有什么高深的理论,也不牵扯到框架. 目前已经越来越多的站点以及wap站点使用响应 ...
- C++设计模式之-工厂模式的总结
工厂模式分为3种,即简单工厂模式.工厂方法模式.抽象工厂模式,其实大同小异,总结下来就是: 简单工厂模式:一个工厂,多个产品.产品需要有一个虚基类.通过传入参数,生成具体产品对象,并利用基类指针指向此 ...
- 多网卡下,vlc发送IGMP组播报告包
这两天测试IGMP遇到一个问题,环境描述如下: 我的vlc客户端安装在windows下,该PC有两张网卡,本地连接1接公司网,本地链接2 接路由器.wireshark坚挺本地链接2,以测试路由的IGM ...
- vue.js 源代码学习笔记 ----- core scedule.js
/* @flow */ import type Watcher from './watcher' import config from '../config' import { callHook } ...
- Linux:vim环境设置
vim环境设置 vim的环境设置在/etc/vimrc的这个文件中,不过不建议直接修改该配置文件.但是可以修改~/.vimrc文件,默认是不存在的,要手动创建并写入设置值. set hlsearch ...
- 监控摄像机常识:宽动态 (WDR)介绍和理解
安装和使用监控摄像机经常会遇到强光问题. 因为我们不可能灵活选择摄像机的安装位置, 解决或者处理强光是一个无法避免的问题. 不管是由反光材质或者灯源造成此反光, 解决问题的方案来自于摄像机支持的一个特 ...
- 42.zip
最近看linux的解压缩,无意间了解到了一个和压缩率相关的小故事——42.zip 一般我们使用压缩工具的时候,都会用到无损压缩技术,对于无损压缩,算法非常重要,不同的算法实现 的压缩率和速度有很大差别 ...
- 安全性测试AppScan工具使用实战20150920
Appscan是做安全性测试的一款工具,网上资料比较少,项目需要做安全性测试,用它做了web的扫描,可以发现一些问题,并且有原因分析和修复建议,感觉还不错,现在实战 1.打开工具,点击[文件]下的[新 ...
- vue项目更改端口号
vue项目的默认端口为8080,有时候处于某些原因会造成端口号的冲突,因此需要我们适当的更改其端口号来解决问题,将上图中的port更改为合适的端口号就可以,但是需要注意的是浏览器的安全限制端口号详情见 ...
- Git详解之九 Git内部原理
以下内容转载自:http://www.open-open.com/lib/view/open1328070620202.html Git 内部原理 不管你是从前面的章节直接跳到了本章,还是读完了其余各 ...