给你一棵树

让你进行切割

问你最多能切多少刀   使得每个连通分量size都是偶数

思路:首先  要是有奇数个节点的话   那么不管你怎么切割  都会有一个连通分量的size是奇数

所以只有偶数的情况才可能进行切割

切割的话  只要切割size为偶数的节点就行    把size为偶数的节点和他的父节点切开   就能保证连通分量的size为偶数

dfs一下就过了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;
const int maxn = 1e5+5;
vector<int> g[maxn];
bool vis[maxn];
int ans=0;
int dfs(int u);
int main()
{
int n,i,j,k;
scanf("%d",&n);
if(n%2)
{
printf("-1\n");
return 0;
}
memset(vis, false, sizeof(vis));
for(i=0;i<n-1;++i)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
// dfs2(1,-1);
dfs(1);
cout << ans << endl;
}
int dfs(int u)
{
vis[u] = true;
int sum = 0;
for(int i=0;i<g[u].size();++i)
{
int v = g[u][i];
if(vis[v] == false)
{
sum+=dfs(v);
}
}
if(sum % 2 == 1 && u != 1)
ans ++;
return sum+1;
}

  

codeforces 982 c的更多相关文章

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

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

  2. Codeforces 982 B. Bus of Characters(模拟一个栈)

    解题思路: 排序之后模拟一个栈(也可以用真的栈),时间复杂度o(n). 代码: #include <bits/stdc++.h> using namespace std; typedef ...

  3. CodeForces 982 C Cut 'em all!

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

  4. CodeForces 982 D Shark

    Shark 题意:一个研究员观察了一条鲨鱼n天的运动,然后这条鲨鱼他只会往前走,不会回到去过的地方,现在有一个k,,如果鲨鱼当天游过的距离 >= k, 代表的鲨鱼在这天会前往下一个地点,现在求鲨 ...

  5. Codeforces 982 树边两端点计数偶数连通块 鲨鱼活动最小K最大location 扩展欧几里得方块内光线反射

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  6. Codeforces Codeforces Round #484 (Div. 2) E. Billiard

    Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...

  7. Codeforces Codeforces Round #484 (Div. 2) D. Shark

    Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D ...

  8. Codeforces Round #484 (Div. 2)

    题目链接:http://codeforces.com/contest/982 A. Row time limit per test:1 second memory limit per test:256 ...

  9. Guard Duty (medium) Codeforces - 958E2 || (bzoj 2151||洛谷P1792) 种树 || 编译优化

    https://codeforces.com/contest/958/problem/E2 首先求出N个时刻的N-1个间隔长度,问题就相当于在这些间隔中选K个数,相邻两个不能同时选,要求和最小 方法1 ...

随机推荐

  1. html标签三

    1.下拉框和下拉表框 <select name="" id="" multiple> <option value="xx" ...

  2. 学习javascript怎么入门,初学者5条建议

    你是否已经初步掌握了html和css,但完全不知道从何入手Java?如果是,这里总结了5条建议,帮助JavaScript初学者总结学习方法,提高学习效率. 一.多看视频少看书 对初学者而言,看书的效率 ...

  3. 引用类型中的push()、pop()、shift()方法

    /** * write by waitingbar1014 * time 2013.10.18 * 用途:常用于如一堆游戏激活码中获得抽取的值 * **/ //有以下一个数组: var arrayli ...

  4. C++中stl的map

    总结: 在map中插入数据有三种方法: 1.用insert插入pair数据: mapstudent.insert(pair<int,string>(1,"studentone&q ...

  5. (转)jquery.cookie中的操作

      jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一个会话cookie: $.cookie(‘cookieName’,'cooki ...

  6. Android.Tools.Summary

    Android平台上工具的总结 每个工具的详细使用和深入理解参考每个工具相关的blog. 1. Android SDK中提供的工具 http://developer.android.com/tools ...

  7. jsp页面错误的全局处理

    网上搜索spring mvc项目全局异常处理: 大致可以找到两种方案 : 方案1: ExceptionHandlerResolver . spring 提供了两种默认实现,当然你也可以自己实现.. 方 ...

  8. system v 共享内存

    #include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> ...

  9. inet_pton和inet_ntop inet_ntoa

    Linux下地址转换函数 inet_pton ==> Ox inet_ntop ==> xxx.xxx.xxx #include <sys/types.h> #include ...

  10. Mvvm Light 无法添加MvvmView(Win81)的问题

    After I create a MvvmLight(Win81) project, I want add a new view , but there is only MvvmView(Win8), ...