题目链接:

https://cn.vjudge.net/problem/1576783/origin

输入输出:

Examples
inputCopy
4
2 4
4 1
3 1
outputCopy
1
inputCopy
3
1 2
1 3
outputCopy
-1
inputCopy
10
7 1
8 4
8 10
4 7
6 5
9 3
3 5
2 10
2 5
outputCopy
4
inputCopy
2
1 2
outputCopy
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

题目大意:

这道题其实题意很简单,给你一棵树,让你删边,然后得到的子树节点个数要是偶数。

而边的个数在离散数学里定义是 m = n-1, 这个在建树的时候需要注意!

DFS的作用是统计这个节点连接的节点的个数,具体实现在代码中有注释。

下面是AC代码:

#include <iostream>
#include <cstdio>
#include <vector>
#define pb push_back
#include <string.h> using namespace std;
const int MX = 1e5+;
int vis[MX], sum[MX];
int n;
vector<int> G[MX]; int dfs(int x)
{
for(int i = ; i < G[x].size(); ++i)
{
int u = G[x][i];
if(!vis[u])
{
vis[u] = ; // 经过的点先标记一下
sum[x] += dfs(u); //沿着点DFS
vis[u] = ; // 回溯
}
}
sum[x]++; // 经过一个点则需要加一
return sum[x];
} int main()
{
int ans = ;
memset(vis, , sizeof(vis));
memset(sum, , sizeof(sum));
scanf("%d", &n);
for(int i = ; i <= n-; ++i) // 建树初始化m = n-1
{
int u, v;
scanf("%d%d", &u, &v);
G[u].pb(v);
G[v].pb(u); // 无向图!
}
if(n%) //节点为奇数则不可能都分为偶数节点的连通图
{
printf("-1\n");
return ;
}
vis[] = ; // 初始节点的vis先标记为一
dfs();
for(int i = ; i <= n; ++i)
{
//cout << sum[i] << ' ';
if(sum[i]% == ) ans++; // 节点个数为偶数则减
}
//cout << endl;
printf("%d\n", ans-); //减一的理由是根节点节点数一定为偶数,但是其不能减。。
}

如有疑问,欢迎评论指出!

Codeforces --- 982C Cut 'em all! DFS加贪心的更多相关文章

  1. codeforces 982C Cut 'em all!

    题意: 给出一棵树,问最多去掉多少条边之后,剩下的连通分量的size都是偶数. 思路: 如果本来就是奇数个点,那么无论去掉多少条边都不可能成立的. 如果是偶数个点,就进行一次dfs,假设一个点的父亲是 ...

  2. CF982C Cut 'em all! DFS 树 * 二十一

     Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Cut 'em all! CodeForces - 982C(贪心dfs)

    K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...

  4. Codeforces 982C(dfs+思维)

    C. Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. CodeForces 982 C Cut 'em all!

    Cut 'em all! 题意:求删除了边之后,剩下的每一块联通块他的点数都为偶数,求删除的边最多能是多少. 题解:如果n为奇数,直接返回-1,因为不可能成立.如果n为偶数,随意找一个点DFS建树记录 ...

  6. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

  7. hdu 4004 (二分加贪心) 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

  8. Codeforces Round #613 (Div. 2)D(贪心,分治)

    构造两颗深度为30的字典树(根节点分别是0和1),结点只有0和1,从根节点向下DFS,贪心取答案. #define HAVE_STRUCT_TIMESPEC #include<bits/stdc ...

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

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

随机推荐

  1. Day047--JS BOM介绍, jQuery介绍和使用

    内容回顾 DOM 文档对象模型(model) 一个模型就是一个对象(属性和方法 面向对象的三大特性:封装 继承 多态) 为了可扩展性 DOM操作 标签属性操作 获取值 getAttribute() 设 ...

  2. 语义化标签和jQuery选择器

    关于语义化标签 https://blog.csdn.net/nongweiyilady/article/details/53885433 更详细的语义化标签:https://www.cnblogs.c ...

  3. Linux运维(首页)

    系统学习,以此见证学习历程 Linux运维基础 安装vmware+centos Linux基础 Linux的一些问题 Ubuntu遇到的bug linux_网易云音乐安装 linux_添加图标 遇到的 ...

  4. Form -- 文件上传

    当我们选中文件,点击上传时即可. 而此按钮一般是一张图片覆盖了一个input标签而以.基于这个原理我们可以定制自己喜欢的样式 <div style="text-align: cente ...

  5. [物理学与PDEs]第1章习题2 均匀带电球面的电场强度与电势

    设有一均匀分布着电荷的半径为 $R$ 的球面, 其电荷密度 (即单位面积上的电荷量) 为 $\sigma$. 试求该球面所形成电场的电场强度及电势. 解答: 设 $P$ 距圆心的距离为 $r$, 不妨 ...

  6. shell 批量检测远程端口

    [DNyunwei@YZSJHL24-209 li]$ cat port.sh #!/bin/bash # ip=`cat iplist` for i in $ip;do port=`ssh -t $ ...

  7. springMVC中controller的几种返回类型

    ==网文1,还不错,感觉比较老旧springMVC中controller的几种返回类型 - CSDN博客http://blog.csdn.net/qq_16071145/article/details ...

  8. C# - 表达式与语句

    表达式与语句(Expression&Statement) 操作数(Operands) 1.数字.2.字符.3.变量.4.类型.5.对象.6.方法 操作符(Operator) 参考:C# - 操 ...

  9. hadoop与hbase对应的支持版本

    官网信息:http://hbase.apache.org/book.html#java JDK的对应关系: 用jdk1.8+hadoop2.7.4/hadoop2.7.6+zookeeper3.4.1 ...

  10. vCenter Server 6 Standard

    准备环境和工具: 三台 ESXi 6.0主机: 准备一台Windows Server 2008 R2系统的虚拟机: VMware-VIM-all-6.0.0.iso 软件下载地址 链接: https: ...