CF982 C Cut 'em all!【树/DFS/思维】
【链接】:CF982C
【题意】:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边。若不能切,输出-1。

【分析】:
1.若点数n为奇数,因为奇数不可能分为偶数,那么一定输出-1
2.若点数n为偶数,偶数=偶数+偶数。就从顶点1开始,当作父顶点开始dfs。dfs用于计算子树的顶点数,如果子树是偶数个顶点,那么ans就可以++,然后把该子树标记成搜索过的,最后的答案要-1;因为整棵树肯定是偶数顶点,ans也会+1;
【代码】:
[不用vis数组]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2*1e5+5;
const ll INF = 2147483647;
typedef pair<ll ,int> pli;
vector<int> G[maxn];
int ans;
int vis[maxn];
int dfs(int x,int fa)
{
int sum=1; //我们在讨论子数结点数一般是包括根结点自身的
for(int i=0; i<G[x].size(); i++)
{
int u = G[x][i];
if(u!=fa)
{
sum+=dfs(u,x);
}
}
if(sum%2==0) ans++;
return sum;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
if(n&1)
{
puts("-1");
return 0;
}
dfs(1,0);
cout<<ans-1<<endl;//原本自己就是偶数,所以要减1
}
[用vis数组]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2*1e5+5;
const ll INF = 2147483647;
typedef pair<ll ,int> pli;
vector<int> G[maxn];
int ans;
int vis[maxn];
int dfs(int x)
{
int sum=1;
vis[x]=1;
for(int i=0; i<G[x].size(); i++)
{
int u = G[x][i];
if(!vis[u])
{
sum+=dfs(u);
}
}
if(sum%2==0) ans++;
return sum;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
if(n&1)
{
puts("-1");
return 0;
}
dfs(1);
cout<<ans-1<<endl;
}
CF982 C Cut 'em all!【树/DFS/思维】的更多相关文章
- Codeforces Round #484 (Div. 2)Cut 'em all!(dfs)
题目链接 题意:给你一棵树,让你尽可能删除多的边使得剩余所有的联通组件都是偶数大小. 思路:考虑dfs,从1出发,若当前节点的子节点和自己的数目是偶数,说明当前节点和父亲节点的边是可以删除的,答案+1 ...
- Codeforces 982 C. Cut 'em all!(dfs)
解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...
- CF982C Cut 'em all! DFS 树 * 二十一
Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Cut 'em all! CodeForces - 982C(贪心dfs)
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...
- Codeforces 982C(dfs+思维)
C. Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- CodeForces 982 C Cut 'em all!
Cut 'em all! 题意:求删除了边之后,剩下的每一块联通块他的点数都为偶数,求删除的边最多能是多少. 题解:如果n为奇数,直接返回-1,因为不可能成立.如果n为偶数,随意找一个点DFS建树记录 ...
- HDU 5692 线段树+dfs序
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s 内存限制:512.0MB 总提交次数:196 AC次数:65 平均分: ...
- 51 nod 1681 公共祖先 (主席树+dfs序)
1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...
随机推荐
- mplab c30 注册方法
http://blog.csdn.net/q553716434/article/details/7459036 关键文件是: C:\Program Files\Microchip\MPLAB C30\ ...
- BZOJ 3545 / 洛谷 P4197 Peaks 解题报告
P4197 Peaks 题目描述 在\(\text{Bytemountains}\)有\(N\)座山峰,每座山峰有他的高度\(h_i\).有些山峰之间有双向道路相连,共\(M\)条路径,每条路径有一个 ...
- codeforces 1015A
A. Points in Segments time limit per test 1 second memory limit per test 256 megabytes input standar ...
- mmall项目之问题一(mavenplugin问题)
在进行mybatis逆向工程到时候,报错,提示maven plugin 错误,提示missing..... 解决办法: 因为之前到pom中忘记了加版本信息,添加后错误消失:
- 毕业答辩的PPT攻略
关于内容: 1.一般概括性内容:课题标题.答辩人.课题执行时间.课题指导教师.课题的归属.致谢等. 2.课题研究内容:研究目的.方案设计(流程图).运行过程.研究结果.创新性.应用价值.有关课题延续 ...
- 数据仓库3级范式(3NF)基础
一.引言 最近在整理理大数据模式下的数据仓库数据模型,资料来自互联网和读过的数据仓库理论和实践相关. 二.3NF (1)1NF-无重复的列 数据库表的每一列都是不可分割的基本数据项,同一列中不能有多个 ...
- bzoj 1861 splay
就是裸地splay,然后自己写的不是特别好,tle了,最近时间比较紧迫,有时间了改下,在此记录 另附转载pascal AC代码最下面 /******************************** ...
- python函数对象和闭包
关于函数对象和闭包 闭包(closure)是函数式编程的重要的语法结构.不同的语言实现闭包的方式不同.Python以函数对象为基础,为闭包这一语法结构提供支持的 (我们在特殊方法与多范式中,已经多次看 ...
- LeetCode 10 Regular Expression Match
'.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...
- redis有string,hash,list,sets.zsets几种数据类型
1.string数据类型 可包含任何数据,是二进制安全的,比如图片或者序列化的对象set key valueset name hkset age 20get name 得到"hk" ...