题目链接

题意:给你一棵树,让你尽可能删除多的边使得剩余所有的联通组件都是偶数大小。

思路:考虑dfs,从1出发,若当前节点的子节点和自己的数目是偶数,说明当前节点和父亲节点的边是可以删除的,答案+1,因为最开始的节点没有父节点,所以最后答案-1

#include<bits/stdc++.h>

#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back using namespace std; LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
const int N = 2e5 +11;
int n;
vector<int>v[N];
int ans=0;
int dfs(int now,int pre){
int su=1;
for(int k:v[now]){
if(k==pre)continue;
su+=dfs(k,now);
}
if(su%2==0)ans++;
return su;
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
if(n&1)return cout<<-1,0;
for(int i=2;i<=n;i++){
int s,t;
cin>>s>>t;
v[s].pb(t);
v[t].pb(s);
}
dfs(1,0);
cout<<ans-1;
return 0;
}

Codeforces Round #484 (Div. 2)Cut 'em all!(dfs)的更多相关文章

  1. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  2. Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)

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

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

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

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

  5. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  6. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  7. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  8. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  9. Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 3000 mSec Problem Descripti ...

随机推荐

  1. jQuery中$.each()方法的使用

    $.each()是对数组,json和dom结构等的遍历,说一下他的使用方法吧. 1.遍历一维数组 var arr1=['aa','bb','cc','dd']; $.each(arr1,functio ...

  2. 记一次在咸鱼上购买 MacBook Pro 的经历

    前言 以前一直用的是 windows 的,但是最近特别想买个 macOS 的.其实不是为了其他什么目的,只是涉及到开发 macOS更接近 linux 系统,一直没使用过所以就想尝试体验下,而且现在很多 ...

  3. 多线程手写Future模式

    future模式 在进行耗时操作的时候,线程直接阻塞,我们需要优化这样的代码,让他再启动一个线程,不阻塞.可以执行下面的代码. 这个时候我们就用到了未来者模式 future设计类 只有一个方法 pub ...

  4. MySQL实现按天统计数据的方法

    一.首先生成一个日期表,执行SQL如下: CREATE TABLE num (i int); ), (), (), (), (), (), (), (), (), (); CREATE TABLE i ...

  5. 企业出口退税申报系统的Sqlite数据库破解及读写

    QQ:564955427 原始出处:https://www.cnblogs.com/Charltsing/p/TSSB.html 最近一些朋友反映龙图的企业出口退税申报系统的Sqlite数据库改变了加 ...

  6. cat

    cat命令能够显示或拼接文件内容,它本身表示conCATenate(拼接) 一般语法 打印单个文件的内容 [root@VM_0_15_centos ~]# cat output.session 打印多 ...

  7. ##Django中Application labels aren't unique解决方法##

    pip更新了所有插件,发现了按平常编码遇到些问题,记录下. Django错误 django.core.exceptions.ImproperlyConfigured: Application labe ...

  8. .net string类型集合转int集合

    1.string集合转int集合 //string类型的集合 List<string> tempStr = new List<string>() { "21" ...

  9. Windows apache-flume-1.6.0+Kafka+Es

    apache-flume-1.6.0 kafka_2.11-1.1.0 elasticsearch1.5.1 flume配置 a1.sources = kafkaSource a1.channels ...

  10. Python进阶10---魔术方法*

    特殊属性 查看属性 #animal.py class Animal: x = 123 def __init__(self,name): self._name = name self.__age = 1 ...