3391: [Usaco2004 Dec]Tree Cutting网络破坏

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 47  Solved: 37
[Submit][Status]

Description

    约翰意识到贝茜建设网络花费了他巨额的经费,就把她解雇了.贝茜很愤怒,打算狠狠报
复.她打算破坏刚建成的约翰的网络.    约翰的网络是树形的,连接着N(1≤N≤10000)个牛棚.她打算切断某一个牛棚的电源,使和这个牛棚相连的所有电缆全部中断.之后,就会存在若干子网络.为保证破坏够大,每一个子网的牛棚数不得超过总牛棚数的一半,那哪些牛棚值得破坏呢?

Input

    第1行:一个整数N.
    第2到N+1行:每行输入两个整数,表示一条电缆的两个端点.

Output

    按从小到大的顺序,输出所有值得破坏的牛棚.如果没有一个值得破坏,就输出“NONE”.

Sample Input

10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8

Sample Output

3
8

如果牛棚3或牛棚8被破坏,剩下的三个子网节点数将是5,2,2,没有超过5的.
来源信息

HINT

 

Source

Silver

题解:

类似与找重心,dfs一遍就行了。

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 10000+10
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,tot,head[maxn],a[maxn],f[maxn],s[maxn];
bool v[maxn];
struct edge{int go,next;}e[*maxn];
inline void insert(int x,int y)
{
e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
e[++tot].go=x;e[tot].next=head[y];head[y]=tot;
}
void dfs(int x)
{
v[x]=;f[x]=;s[x]=;
for(int i=head[x],y;i;i=e[i].next)
if(!v[y=e[i].go])
{
dfs(y);
s[x]+=s[y];
f[x]=max(f[x],s[y]);
}
f[x]=max(f[x],n-s[x]);
if(f[x]<=n/)a[++a[]]=x;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n-)insert(read(),read());
dfs();
if(!a[]){printf("NONE");return ;}
sort(a+,a+a[]+);
for1(i,a[])printf("%d\n",a[i]);
return ;
}

BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏的更多相关文章

  1. 【树形dp】Bzoj3391 [Usaco2004 Dec]Tree Cutting网络破坏

    Description     约翰意识到贝茜建设网络花费了他巨额的经费,就把她解雇了.贝茜很愤怒,打算狠狠报 复.她打算破坏刚建成的约翰的网络.    约翰的网络是树形的,连接着N(1≤N≤1000 ...

  2. 【枚举】bzoj3391 [Usaco2004 Dec]Tree Cutting网络破坏

    #include<cstdio> using namespace std; #define N 10001 int n; int v[N<<1],first[N],next[N ...

  3. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )

    因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...

  4. 3391: [Usaco2004 Dec]Tree Cutting网络破坏

    3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 76  Solved: 59[ ...

  5. 【BZOJ】3391: [Usaco2004 Dec]Tree Cutting网络破坏(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3391 显然判断每个点只需要判断子树是否小于等于n/2即可 那么我们虚拟一个根,然后计算每个子树的si ...

  6. BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏(搜索)

    这道直接遍历一遍求出每个点的子节点数目就行了= = CODE: #include<cstdio>#include<iostream>#include<algorithm& ...

  7. BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏(树形DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3391 [题目大意] 给定一棵树,求分支size均不大于一半点数的点 [题解] 递归的同 ...

  8. BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏:dfs【无根树 节点分枝子树大小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3391 题意: 给你一棵无根树,求分支size均不大于一半点数的点. 题解: 假定1为根. ...

  9. BZOJ 3391 Tree Cutting网络破坏

    不想写. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> ...

随机推荐

  1. Java Performance Optimization Tools and Techniques for Turbocharged Apps--reference

    Java Performance Optimization by: Pierre-Hugues Charbonneau reference:http://refcardz.dzone.com/refc ...

  2. mysql myisam 锁表问题<转>

    转自http://yafei001.iteye.com/blog/1841258 锁是计算机协调多个进程或线程并发访问某一资源的机制 .在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用 ...

  3. 配置ISCSI服务器

    一.在linux下安装启动iscsi target 1.安装启动iscsi服务 [root@wjb10000 ~]# yum -y install targetcli.noarch 2.建立一个目录设 ...

  4. C++ STL set集合容器

    汇总了一些set的常用语句,部分参考了这篇:http://blog.163.com/jackie_howe/blog/static/199491347201231691525484/ #include ...

  5. 执​行​o​r​a​c​l​e​函​数​的​四​种​方​法

    1.在定义函数时:如果有参数,则参数可有类型但是不加长度. 2.在执行函数: var/variable var_name var_type(如果数据类型是number则没有长度,如果数据类型是varc ...

  6. SQL Server 2005、SQL Server 2008版本比较

    SQL Server 2005的版本有SQL Server 2005企业版(Enterprise).SQL Server 2005标准版(Standard) 和SQL Server 2005工作组版( ...

  7. Linux下快速搭建DNS服务器

    一.术语解释:TTL Time To Live 缓冲保留时间ORIGIN 属于哪个域@ 代指域IN 开头需要空格SOA 一行记录类型的开始参数:forwarders {} 指向自己无法解析的域名跳转到 ...

  8. 去掉eclipse上编辑时的提示

    用eclipse时,鼠标移到类上时会给出提示,如下图:

  9. java数据流

    DataInputStream和DataOutputStream提供了可以直接存取java基本类型(int,double等)的方法.对于存取基本类型,在效率上比普通字节流要快很多.它们分别继承inpu ...

  10. 浏览器d判断

    1.判断浏览器类型 if navigator.userAgent.indexOf(”MSIE”)>0) {} //判断是否IE浏览器 if(isFirefox=navigator.userAge ...