POJ 2378 Tree Cutting 子树统计
来源:互联网 发布:linux安装包命令 编辑:程序博客网 时间:2024/06/05 05:36
题目大意:给出一棵树,将树中的一个节点去掉之后,这棵树会分裂成一些联通块,求去掉哪些点之后,所有联通块的大小不超过所有节点的一半,并按顺序输出。
思路:基础的子树统计问题,只要深搜一遍就可以出解。这个步骤和求树的重心很像,是树分治的基础。
CODE:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 10010using namespace std;int points;int head[MAX],total;int next[MAX << 1],aim[MAX << 1];int size[MAX];bool ans[MAX];inline void Add(int x,int y);void DFS(int x,int last);int main(){cin >> points;for(int x,y,i = 1;i < points; ++i) {scanf("%d%d",&x,&y);Add(x,y),Add(y,x);}DFS(1,0);for(int i = 1;i <= points; ++i)if(ans[i])printf("%d\n",i);return 0;}inline void Add(int x,int y){next[++total] = head[x];aim[total] = y;head[x] = total;}void DFS(int x,int last){size[x] = 1;int max_size = 0;for(int i = head[x];i;i = next[i]) {if(aim[i] == last)continue;DFS(aim[i],x);size[x] += size[aim[i]];max_size = max(max_size,size[aim[i]]);}max_size = max(max_size,points - size[x]);if(max_size <= (points >> 1))ans[x] = true;}
0 0
- POJ 2378 Tree Cutting 子树统计
- poj 2378 Tree Cutting
- POJ 2378 Tree Cutting
- POJ 2378 Tree Cutting
- poj 2378 Tree Cutting
- POJ 2378 Tree Cutting
- poj 2378 Tree Cutting
- poj 2378 Tree Cutting
- [POJ 2378] Tree Cutting
- POJ-2378-Tree cutting
- POJ 2378 Tree Cutting 1655 Blancing Act
- POJ--2378--Tree Cutting--树形DP
- poj 2378 Tree Cutting(树形dp)
- (简单) 树形dp POJ 2378 Tree Cutting
- poj 2378 Tree Cutting (树形dp)
- poj 2378 Tree Cutting 树形dp
- Tree Cutting - POJ 2378 树形dp
- POJ 2378 Tree Cutting(树形DP)
- 【Interview】【C++】几道笔试题
- hdu 4777 Rabbit Kingdom(树状数组)
- VS2013中安装配置Boost库
- ActivityStackSupervisor分析
- Halcon/C++使用技巧
- POJ 2378 Tree Cutting 子树统计
- 傅里叶变换理解之三
- hdu 5062 Beautiful Palindrome Number(水题)
- C++中的单例模式
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--客户端对象模型和REST APIs概览 客户端对象模型(CSOM)基础
- Python简单文本处理(一)
- hdu 5063 Operation the Sequence(模拟)
- 如何使用GCD
- 走进大学殿堂 开启大学时光