Codeforces --- 982C Cut 'em all! DFS加贪心
题目链接:
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加贪心的更多相关文章
- codeforces 982C Cut 'em all!
题意: 给出一棵树,问最多去掉多少条边之后,剩下的连通分量的size都是偶数. 思路: 如果本来就是奇数个点,那么无论去掉多少条边都不可能成立的. 如果是偶数个点,就进行一次dfs,假设一个点的父亲是 ...
- 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建树记录 ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- hdu 4004 (二分加贪心) 青蛙过河
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...
- Codeforces Round #613 (Div. 2)D(贪心,分治)
构造两颗深度为30的字典树(根节点分别是0和1),结点只有0和1,从根节点向下DFS,贪心取答案. #define HAVE_STRUCT_TIMESPEC #include<bits/stdc ...
- Codeforces 982 C. Cut 'em all!(dfs)
解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...
随机推荐
- Codeforces 1037E Trips
原题 题目大意: 有\(n\)个人,起初他们都不是朋友.总共有\(m\)天,每天会有两个人成为朋友.他们计划在晚上出去旅游,对于一个人,有如下两种情况: 1.要么他不出去旅游 2.要么有至少\(k\) ...
- ElasticSearch6.5.0 【Java客户端之REST Client】
说明 High Level Client 是基于 Low Level Client 的.官方文档如下: * https://www.elastic.co/guide/en/elasticsearch/ ...
- qml : qml控件自适应;
import QtQuick 2.4 Item { property var targetItem: parent property bool fixedAspectRatio: true // El ...
- go 数组 切片 字典 结构体
数组 ##数组的定义与赋值: 1. var num [3]int num = [3]int{1,2,3} 2. var num [3]int = [3]int {1,2,3} 3. num := [3 ...
- openstack项目【day24】:KVM部署
本节内容 虚拟化支持 软件准备 检查CPU虚拟化支持 安装软件包 激活并启动libvirtd服务 网络模式 配置桥接网络 验证网络 尝试连接Hypervisor 创建虚拟机 虚拟机操作 一.虚拟化支持 ...
- Error creating bean with name
最近在学一个东西,要使用SSM新建一个案例,是这样滴,我有如下 DeptDAO DeptService DeptServiceImpl DeptController Dept Mybatis 首先,我 ...
- 用 Mathematica 获取图片的 RGB 三基色
ColorConvert[*, "RGB"] // InputForm 其中 * 表示你把你的图片拖入 Mathematica 中.
- 液晶流在齐次 Besov 空间中的正则性准则
在 [Zhang, Zujin. Regularity criteria for the three dimensional Ericksen–Leslie system in homogeneous ...
- tf.py_func
在 faster rcnn的tensorflow 实现中看到这个函数 rois,rpn_scores=tf.py_func(proposal_layer,[rpn_cls_prob,rpn_bbox ...
- C++中数组作为形参进行传递(转)
有两种传递方法,一种是function(int a[]); 另一种是function(int *a) 这两种两种方法在函数中对数组参数的修改都会影响到实参本身的值! 对于第一种,根据之前所学,形参是实 ...