题目链接:

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. 如何取消Paypal自动付款功能

    在国外在线服务消费肯定会常遇到PayPal的支付方式,有些人可能PayPal有些余额可能会用这个工具来支付,但付款后,可能服务因为不满意而退掉,但第二年却自动续约了?但明明服务已退掉,这该怎么处理呢? ...

  2. beam 的异常处理 Error Handling Elements in Apache Beam Pipelines

    Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...

  3. 越光后端开发——ygapi(1.新建项目ygapi、新建MySQL数据库yg、项目连接数据库)

    1.新建MySQL数据库 show databases;//查看已经有的数据库 create database yg; 2.新建项目ygapi 1.使用pycharm新建django项目取名ygapi ...

  4. (二叉树 递归 DFS) leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  5. python 学习地址

    本章主要记录学习python过程中借鉴的一些网站,并感谢这些博主辛勤付出. python官网:https://www.python.org/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  6. Pandas系列(六)-时间序列详解

    内容目录 1. 基础概述 2. 转换时间戳 3. 生成时间戳范围 4. DatetimeIndex 5. DateOffset对象 6. 与时间序列相关的方法 6.1 移动 6.2 频率转换 6.3 ...

  7. 从Socket入门到BIO,PIO,NIO,multiplexing,AIO(未完待续)

    Socket入门 最简单的Server端读取Client端内容的demo public class Server { public static void main(String [] args) t ...

  8. Java线程安全队列BlockingQueue

    线程安全队列BlockingQueue 用法跟普通队列没有区别,只是加入了多线程支持. 这里主要说说add和put,以及poll和take的区别: add和put都是用来忘队列里面塞东西的,而poll ...

  9. ve2.0 v-for循环报错的解决方案

    <li v-for="(item,index) in mokeData" class="page" :key="index"> ...

  10. jquery 查找子窗口

    $("#订单信息").contents().find('div.datagrid-body').hide();$("#订单信息").contents().fin ...