ACdream 1031 Cut(搜索 树)
来源:互联网 发布:浙江省数据开放平台 编辑:程序博客网 时间:2024/06/09 23:03
B - Cut
Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Status
Problem Description
Cut(in another word, remove) edges the given tree \(T\) so that each connected component has even number of nodes.
Find out the maximum number of removed edges.
Input
The first line contains one integer \(n\), which denotes the number of nodes of tree \(T\).
The following \((n - 1)\) lines with two integers \(a_i, b_i\), which denotes edge \((a_i, b_i)\).
Note that the nodes are labled by \(1, 2, \ldots, n\).
(\(1 \leq n \leq 10^5\), \(n\) is even)
Output
The only integer shows the maximum number of removed edges.
Sample Input
41 22 33 4
Sample Output
1
Hint
Only edge $(2, 3)$ can be removed.
题意: 给你n个节点的树 你的任务是删除边使得 偶数块尽量的多。
思路: 比较简单的一个树上的搜索。。 思路就是从一个节点开始搜索,然后如果碰见某一个节点的 孩子个数为 偶数 就删除这个
节点和他父亲之间的边(其实实现的时候 不用真的删除 )并且最终结果加一。
代码:
#include<stdio.h>#include<string.h>#include<vector>#include<iostream>#include<algorithm>#define N 100005using namespace std;vector< int >edge[N];int n;int cnt;int dfs(int u,int fa){int sum=0;for(int i=0;i<edge[u].size();i++){int v=edge[u][i];if(v==fa) continue;int num=dfs(v,u);//printf("v: %d %d\n",v,num);if(num%2==1) sum+=num;else cnt++;}//printf("%d %d\n",u,sum);return sum+1;}int main(){int u,v; scanf("%d",&n);for(int i=1;i<n;i++){scanf("%d %d",&u,&v);edge[u].push_back(v);edge[v].push_back(u);}cnt=0;dfs(1,-1);printf("%d\n",cnt);return 0;}
阅读全文
0 0
- ACdream 1031 Cut(搜索 树)
- ACdream 1031 Cut
- acdream 1142 搜索
- Acdream sgx和路飞(二分搜索)
- ACdream 1187 Rational Number Tree(搜索:DFS)
- 11782 - Optimal Cut(树形DP+记忆化搜索)
- Acdream 1063 平衡树(字典树)
- ACdream 1063 平衡树 (Trie树)
- acdream群赛(树状数组,Trie树)
- Acdream 1065 同心树(数学)
- ACdream 1427 Nice Sequence(线段树)
- [ACdream]哗啦啦村的日常游戏(一)抓个球[概率DP][记忆化搜索]
- ACdream 同心树
- ACDream 1063 平衡树
- ACdream - 1063平衡树
- ACdream 1063 平衡树
- ACdream 1417(Number)
- ACdream发电站(贪心)
- JavaWeb项目第三次总结_成绩查询的实现
- 接口作为数据类型的应用
- hive文件格式
- ORA-39006: internal error ORA-39213: Metadata processing is not available
- memory management unit
- ACdream 1031 Cut(搜索 树)
- 10,25作业
- 数据库的创建
- ASCII中LF与CR区别(换行符)
- 2017.10.21 C组比赛总结
- JavaEE常用API【Date、DateFormat、Calendar、Math、System、包装类、正则表达式】总结
- python-requests库的基本使用教程
- if else和switch case
- 最近一次给民营企业的咨询的感想