首页 技术 正文
技术 2022年11月15日
0 收藏 784 点赞 4,838 浏览 868 个字

题意:给定一棵树,删除一些边,让整棵树被分成多个节点数为偶数的联通块,且联通块尽量多。

思路:如果出现连通且节点数为偶数的立即删除这个点与它父节点之间的边,尽量删除即可,因为题目说了保证n为偶数,删了偶数,剩下的还是偶数。

AC代码

#include <cstdio>#include <cmath>#include <cctype>#include <algorithm>#include <cstring>#include <utility>#include <string>#include <iostream>#include <map>#include <set>#include <vector>#include <queue>#include <stack>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000")#define eps 1e-10#define inf 0x3f3f3f3f#define PI pair<int, int>typedef long long LL;const int maxn = 1e5 + 5;vector<int>G[maxn];int ans;int dfs(int u, int pre) {int cnt = 0;for(int i = 0; i < G[u].size(); ++i) {int v = G[u][i];if(v == pre) continue;int w = dfs(v, u);if(w & 1) cnt++;else ans++;}return cnt+1;}int main() {int n;while(scanf("%d", &n) == 1) {for(int i = 1; i <= n; ++i) G[i].clear();int u, v;for(int i = 0; i < n-1; ++i) {scanf("%d%d", &u, &v);G[u].push_back(v);G[v].push_back(u);}ans = 0;dfs(1, -1);printf("%d\n", ans);}return 0;}

如有不当之处欢迎指出!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289