【BZOJ】3391: [Usaco2004 Dec]Tree Cutting网络破坏(dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=3391
显然判断每个点只需要判断子树是否小于等于n/2即可
那么我们虚拟一个根,然后计算每个子树的size,而这个点的子树的size和n-这个点的size就是我们需要找的
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=10005;
int sz[N], ihead[N], vis[N], mark[N], n, cnt;
struct ED { int to, next; }e[N+N];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
}
void dfs(int x) {
sz[x]=vis[x]=1;
int v, mx=0;
for(int i=ihead[x]; i; i=e[i].next) if(!vis[v=e[i].to]) {
dfs(v);
sz[x]+=sz[v];
mx=max(sz[v], mx);
}
mx=max(mx, n-sz[x]);
if(mx<=(n>>1)) mark[x]=1;
} int main() {
read(n);
for1(i, 1, n-1) add(getint(), getint());
dfs((n+1)>>1);
for1(i, 1, n) if(mark[i]) printf("%d\n", i);
return 0;
}
Description
Input
Output
Sample Input
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8
Sample Output
8
如果牛棚3或牛棚8被破坏,剩下的三个子网节点数将是5,2,2,没有超过5的.
来源信息
HINT
Source
【BZOJ】3391: [Usaco2004 Dec]Tree Cutting网络破坏(dfs)的更多相关文章
- BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )
因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...
- BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏(树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3391 [题目大意] 给定一棵树,求分支size均不大于一半点数的点 [题解] 递归的同 ...
- BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏:dfs【无根树 节点分枝子树大小】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3391 题意: 给你一棵无根树,求分支size均不大于一半点数的点. 题解: 假定1为根. ...
- BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏(搜索)
这道直接遍历一遍求出每个点的子节点数目就行了= = CODE: #include<cstdio>#include<iostream>#include<algorithm& ...
- 3391: [Usaco2004 Dec]Tree Cutting网络破坏
3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 76 Solved: 59[ ...
- BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏
3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 47 Solved: 37[ ...
- 【树形dp】Bzoj3391 [Usaco2004 Dec]Tree Cutting网络破坏
Description 约翰意识到贝茜建设网络花费了他巨额的经费,就把她解雇了.贝茜很愤怒,打算狠狠报 复.她打算破坏刚建成的约翰的网络. 约翰的网络是树形的,连接着N(1≤N≤1000 ...
- 【枚举】bzoj3391 [Usaco2004 Dec]Tree Cutting网络破坏
#include<cstdio> using namespace std; #define N 10001 int n; int v[N<<1],first[N],next[N ...
- BZOJ 3391 Tree Cutting网络破坏
不想写. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> ...
随机推荐
- js取消radio选中 反选
var radio=document.createElement("input");radio.type="radio";radio.onclick = fun ...
- 个人博客平台 http://craft6.cn 上线
以后主要在该个人博客平台发表博文,有兴趣的读者可以访问: Craft6.cn 该博客是自主开发,功能和内容均在逐步增加中.所有文章均是原创.
- Win7 + vs2012 + cocos2d-x2.2 配置开发环境
昨天開始打算学习Cocos2d-x,首先肯定是要在自己的电脑上配置开发环境.昨天折腾了一天,以下将自己在当中遇到的问题与解决方法跟大家分享一下.大多数会遇到的问题,我都遇到了.... 1.安装 ...
- Solr 缓存配置
http://www.blogjava.net/xiaohuzi2008/archive/2012/12/03/392376.html
- win8安装Visual C++ 2015 build tools闪退解决办法
win8安装Visual C++ 2015 build tools闪退解决办法 安装Visual Studio 2015闪退问题也同样应用此解决办法. 1.控制面板——添加删除程序——启动关闭wind ...
- python selenium --browser 操作
本节知识点: 打印URL 将浏览器最大化 设置浏览器固定宽.高 操控浏览器前进.后退 打印URL 上一节讲到,可以将浏览器的title打印出来,这里再讲个简单的,把当前URL打印出来.其实也没啥大用, ...
- <<Python基础教程>>学习笔记 | 第11章 | 文件和素材
打开文件 open(name[mode[,buffing]) name: 是强制选项,模式和缓冲是可选的 #假设文件不在.会报以下错误: >>> f = open(r'D:\text ...
- 阿里云slb实现多域名https
刚开始接锅,没注意原来站点的https配置在slb上,在服务器上配置一顿操作猛如虎,细细检查一遍,感觉良好,一测试发现不对劲,检查发现原来https配置在阿里云slb上,阿里云还是做得不错的 ,但是现 ...
- mac svn 命令
https://www.cnblogs.com/luckythan/p/4478706.html http://blog.csdn.net/aizhiqiang2/article/details/53 ...
- Wpf 自定义控件(1)
1. 新建一个wpf工程,在工程下面新建 一个文件夹themes,在themes下新建两个资源字典文件generic.xaml和PrettySeekBar.xaml generic.xaml ...