【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> ...
随机推荐
- C#秘密武器之多线程——基础
多线程概述 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程? 线程是程序中的一个执行流,每个线程 ...
- 3dmax做的模型导入U3d后 当模型靠近摄像机时镂空问题
使用3dMax Reset XForm下就好了. 原因可能是 法线方向问题?
- 如何查看LoadRunner虚拟用户(vuser)类型
查看vuser类型LoadRunner提供了多种Vuser技术,通过这些技术可以在使用不同类型的客户端/服务器体系结构时生成服务器负载.每种Vuser技术都适合于特定体系结构并产生特定的Vuser类型 ...
- mybatis 表情存储报错问题解决
修改mysql配置文件my.cnf(windows为my.ini) mac brew方式在/usr/local/etc/my.cnf my.cnf一般在etc/mysql/my.cnf位置.找到后请在 ...
- 小强升职记(GTD)脑图
- LaTeX 中插入图片使其紧跟插入的文字之后
LaTeX 中插入图片使其不跑到每页的开头而紧跟插入的文字之后. 此次建模过程中,遇到的一个比较棘手的问题是: 当插入图片时,图片的位置总是会自动跑到当页(或下一页)的最上方,而不是紧跟在其对应的说明 ...
- C# 遍历Dictionary并修改其中的Value
C#的Dictionary类型的值,知道key后,value可以修改吗?答案是肯定能修改的.我在遍历的过程中可以修改Value吗?答案是也是肯定能修改的,但是不能用For each循环.否则会报以下的 ...
- Ant + ivy的安装
有了Ivy的帮忙,我们不需要为了一个库依赖管理而舍弃Ant去学那个难搞的Maven了. 基本配置步骤如下: 1.copy Ivy插件到ant_home/lib下: ivy安装 简单的安装方法: 直 ...
- Linux系统编程之----》信号
"===信号========================================================================================= ...
- c#删除 list中的元素和怎么去除空元素
; i >= ; i--) { if (list[i].NO == item.NO) { list.RemoveAt(i); } } public void RemoveItemFromList ...