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 ...
随机推荐
- 解决RSA加密中,System.Security.Cryptography.CryptographicException: 系统找不到指定的文件
首先说下环境,win2008R2,iis7.5 遇到这个问题,困扰了我一天,在外国的网站上找到答案,还好有点英文基础.最后算是解决了,不过其中的原理还是没有搞的十分清楚. 先说下解决办法, 打开IIS ...
- 前端之DOM
老师的博客:https://www.cnblogs.com/liwenzhou/p/8011504.html DOM(Document Object Model)是一套对文档的内容进行抽象和概念化的方 ...
- On the structure of submodule of finitely generated module over PID
I was absorbed into this problem for three whole days......
- codeforces#1152D. Neko and Aki's Prank(dp)
题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...
- mysql数据库连接语句一定要加传参的编码格式
jdbc:mysql://localhost:3306/pachong?useUnicode=true&characterEncoding=UTF-8 不然可能会出现从源代码传出的中文参数到m ...
- 在Asp.Net Core中集成ABP Dapper
在实际的项目中,除了集成ABP框架的EntityFrameworkCore以外,在有些特定的场景下不可避免地会使用一些SQL查询语句,一方面是由于现在的EntityFrameworkCore2.X有些 ...
- A/B test
A/B test https://en.wikipedia.org/wiki/A/B_testing A/B testing (bucket tests or split-run testing) i ...
- vue 源代码创建tabs
<ul class="tabs"> <li class="li-tab" v-for="(item,index) in tabsPa ...
- P1744 采购特价商品
原题链接 https://www.luogu.org/problemnew/show/P1744 一道最短路的模板题.....很简单吧 求最短路的方法有很多,但是对于刚学完Floyd的我,只会用这个. ...
- 告别回调,拥抱async await
之前使用jquery中ajax,请求的结果需要写在回调函数里面,后面接触到了axios/fetch,使用了es6中Promise进行封装,这种链式结构调用,看起来比之前直观,可是还是没有解决回调的问题 ...