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 ...
随机推荐
- Scala操作Hbase空指针异常java.lang.NullPointerException处理
Hbase版本:Hortonworks Hbase 1.1.2 问题描述:使用Scala操作Hbase时,发生空指针异常(java.lang.RuntimeException: java.lang.N ...
- css子元素添加绝对定位,不添加top、left会有影响吗???
子元素设置absolue,不设置top以及left值会有什么影响呢? 代码如下: .parent { width: 500px; height: 500px; overflow: hidden; ...
- 04 前端篇(JQuery)
jquery: http://www.cnblogs.com/yuanchenqi/articles/5663118.html 优点:简洁.兼容 jquery 对象: jQuery 或 $ 基本 ...
- Bloom Filter(布隆过滤器)如何解决缓存穿透
本文摘抄自我的微信公众号"程序员柯南",欢迎关注!原文阅读 缓存穿透是什么? 关于缓存穿透,简单来说就是系统处理了大量不存在的数据查询.正常的使用缓存流程大致是,数据查询先进行缓存 ...
- iframe知识点详解
<iframe>标签规定一个内联框架,一个内联框架被用来在当前HTML文档中嵌入另一个文档. 1. 常用属性 2. 主要API 3. 轮询 4. 长轮询 5. 自适应 6. 安全性 7. ...
- java中的“空格”用trim()无法去除?原来是这样!
原因: 从txt文件中读取一些数据导入mysql数据库,导入数据库之后发现有一个字段的前面有两个“空格”,后来在代码里我尝试用trim().replace()等方法去除,发现怎么也去不掉,于是我将字符 ...
- pytorch识别CIFAR10:训练ResNet-34(微调网络,准确率提升到85%)
版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 在前一篇中的ResNet-34残差网络,经过训练准确率只达到80%. 这里对网络做点小修改,在最开始的 ...
- jmeter学习记录--09--命令行运行与生成报告
一. 使用命令行方式运行Jmeter 1.1 为什么 使用GUI方式启动jmeter,运行线程较多的测试时,会造成内存和CPU的大量消耗,导致客户机卡死. 所以正确的打开方式是在GUI模式下调 ...
- js工具方法
获取页面链接中的参数,以数组形式返回: function getParamsFromHref() { var parameters = window.location.search.substr(1) ...
- salesforce apex class call exteral webservice
在项目中需要调用外面的Webservice, 从Salesforce往外写入其他系统.目前一般有两种方法. 1. 根据对方提供的wsdl文件生成apex class,直接实例化后调用其方法(测试成功 ...