Codeforces Round #484 (Div. 2)Cut 'em all!(dfs)
题目链接
题意:给你一棵树,让你尽可能删除多的边使得剩余所有的联通组件都是偶数大小。
思路:考虑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)的更多相关文章
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)
- Codeforces Codeforces Round #484 (Div. 2) E. Billiard
Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...
- Codeforces Codeforces Round #484 (Div. 2) D. Shark
Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 3000 mSec Problem Descripti ...
随机推荐
- EF Core使用笔记(基于MySql数据库)
一.什么是EF Entity Framework 是适用于.NET 的对象关系映射程序 (O/RM). 二.比较 EF Core 和 EF6 1.Entity Framework 6 Entity F ...
- ssrfme 复现
这道题搞了我很长时间,主要太菜了,开始复现吧 <?php $sandbox = "sandbox/" . md5("orange" . $_SER ...
- css display和vertical-align 属性
display 定义和用法 display 属性规定元素应该生成的框的类型. 实例 <html> <head> <style type="text/css&qu ...
- 周末学习笔记——day02(带参装饰器,wraps修改文档注释,三元表达式,列表字典推导式,迭代器,生成器,枚举对象,递归)
一,复习 ''' 1.函数的参数:实参与形参 形参:定义函数()中出现的参数 实参:调用函数()中出现的参数 形参拿到实参的值,如果整体赋值(自己改变存放值的地址),实参不会改变,(可变类型)如果修改 ...
- Windows之常用命令
1. 重启/关机 shutdown命令 #关机 shutdown -s -t -f #重启 shutdown -r -t //30秒之后,重启 00是立即 #注销 shutdown -l -t #休眠 ...
- LR socket协议脚本
socket协议分为TCP.UDP两种(区别与联系在此不做赘述),一种为长连接.一种为短连接.如果创建连接时在init中对应关闭连接在end中,则为长连接:如果创建关闭连接都是在action则为短连接 ...
- xml错误之cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
今天从svn导入项目的时候,一个xml文件里面报错:‘cvc-complex-type.2.4.c: The matching wildcard is strict, but no declarati ...
- SQL进阶随笔--case用法(二)
---恢复内容开始--- 用 CHECK 约束定义多个列的条件关系 今天来说下check和case的用法.其实,CASE 表达式和 CHECK 约束是很般配的一对组合.也许有很多数据库工程师不怎么用 ...
- consumer zookeeper is not a recognized option
kafka 创建消费者报错 consumer zookeeper is not a recognized option 在做kafka测试的时候,使用命令bin/kafka-console-consu ...
- centos7之zabbix简单检查之端口监控
1.创建模板 模板名字可以随便起 2.重点在监控项和触发器上. 3.比如我们创建一个监控25端口号的监控项 net.tcp.service[tcp,,25] 这个选项不是手动写上去的,是需要点击选择. ...