【POJ】2378
来源:互联网 发布:西南交大网络教育网站 编辑:程序博客网 时间:2024/06/06 00:25
http://poj.org/problem?id=2378
给定一棵树,求分支size均不大于一半点数的点
递归的同时计算各个分支size,如果出现大于一半点数的则给这个点打上标记
最后输出没有标记的点即可。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <numeric>#include <algorithm>using namespace std;int n;vector <int> v[10005];int size[10005],mark[10005];void dfs(int x,int fx){ size[x]=1; mark[x]=1; for (int i=0;i<v[x].size();i++){ int y=v[x][i]; if (y!=fx){ dfs(y,x); if (size[y]*2>n) mark[x]=0; size[x]+=size[y]; } } if ((n-size[x])*2>n) mark[x]=0;}int main(){ while (cin >> n){ for (int i=1;i<n;i++){ int x,y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } dfs(1,0); bool flag=1; for (int i=1;i<=n;i++){ if (mark[i]){ cout << i << endl; flag=0; } } if (flag) cout << "NONE" << endl; }}
阅读全文
0 0
- POJ 2378
- poj 2378
- 【POJ】2378
- poj 1655树形dp && poj 3107 &&poj 2378 && poj 3140
- poj 2378 Tree Cutting
- poj 2378 树形 dp
- POJ 2378 Tree Cutting
- poj 2378 树型DP
- POJ 2378 Tree Cutting
- poj 2378 Tree Cutting
- poj 2378 树型dp
- poj 2378 水题
- POJ 2378 树形dp
- POJ 2378 Tree Cutting
- poj 2378(树形dp)
- poj 2378 Tree Cutting
- poj 2378 Tree Cutting
- [POJ 2378] Tree Cutting
- 关于this和super关键字为什么不能再static方法中或者静态代码块中
- 记我和ACM的初遇
- 二叉树的深度优先搜索
- 配置 struts-2.3.dtd 但一直无法提示
- 二叉树的广度优先搜索
- 【POJ】2378
- 人类社会与其发展规律
- 文档流
- OpenCV练习1:使用自定义类实现视频和帧处理的接口
- 树莓派更新源
- 代码面试指南学习记录-第一章02
- Onethink上传服务器后登录不了的问题
- GDAL裁剪tif图(保留经纬度)
- java 选择排序