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

-------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int maxn = 10000 + 5;
 
struct edge {
int to;
edge* next;
};
 
edge* pt , EDGE[ maxn << 1 ];
edge* head[ maxn ];
 
void edge_init() {
pt = EDGE;
clr( head , 0 );
}
 
void add( int u , int v ) {
pt -> to = v;
pt -> next = head[ u ];
head[ u ] = pt++;
}
#define add_edge( u , v ) add( u , v ) , add( v , u )
 
 
int n;
int size[ maxn ] , Max;
bool ans[ maxn ];
 
void dfs( int x , int fa ) {
size[ x ] = 1;
for( edge* e = head[ x ] ; e ; e = e -> next ) {
int to = e -> to;
if( to == fa ) continue;
dfs( to , x );
size[ x ] += size[ to ];
if( size[ to ] > Max ) ans[ x ] = false;
}
if( n - size[ x ] > Max ) ans[ x ] =false;
}
int main() {
freopen( "test.in" , "r" , stdin );
edge_init();
clr( ans , -1 );
cin >> n;
rep( i , n - 1 ) {
int u , v;
scanf( "%d%d" , &u , &v );
u-- , v--;
add_edge( u , v );
}
Max = n >> 1;
dfs( 0 , -1 );
bool flag = false;
rep( i , n ) if( ans[ i ] ) {
printf( "%d\n" , i + 1 );
flag = true;
}
if( ! flag ) printf( "NONE\n" );
return 0;
}

-------------------------------------------------------------------------------------

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

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 80  Solved: 63
[Submit][Status][Discuss]

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

BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )的更多相关文章

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

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

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

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

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

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

  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. BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏

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

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

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

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

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

  9. BZOJ 3391 Tree Cutting网络破坏

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

随机推荐

  1. eclipse SVN 安装

    1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java EE Developers)版    如果没有安装的请到这里下载安装:http://ecli ...

  2. Mac OS X 下 TAR.GZ 方式安装 MySQL 5.7+

     方法: http://www.widlabs.com/article/mac-os-x-install-mysql-57-with-tar-gz  mysql下载地址:http://www.mysq ...

  3. Java垃圾回收初步理解

    Java技术提供了一个系统级的线程,即垃圾收集器线程(线程优先级低),垃圾收集线程在一个Java程序中的执行是自动的,不能强制执行, 可以手动调用java.lang.System.gc(),通知JVM ...

  4. Object-c Block的使用及说明

    Object-c 中的block就好像一段C函数般,由函数名,有返回值,有参数,由函数体等 1.简单的block ^(int A ,int B) { int C=A*B; return C; }; 上 ...

  5. 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录

    1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误 解决办法很简单,不能让list为空 2 刚进入该界面的时候list是有数据的,当点击提 ...

  6. beini破解无线

    软件介绍 当你的笔记本有无线网卡却不能上网的时刻,也许你会很焦急. 又或许你的隔壁就有无线网络可以接的时刻,但你却由于米有密码而不能上网.下面我将简介一款可以令你惊讶的软件,奶瓶 有了奶瓶以上疑问都可 ...

  7. leetcode第一刷_Path Sum II

    在更新上面一道题的时候我就想,是不是另一道打印路径的,果不其然啊. 这样的题非经常见的,做法也非常easy,我是用一个引用的vector来存,满足条件之后直接压入结果集中,当然也能够用数组之类的,都一 ...

  8. js中使用this的一些注意事项

    先看看这段代码 var x =9; var fobj ={ x:1, test:function(callback){ var x= 2; callback(); } } function pp(){ ...

  9. JDBC初步(一)

    import java.sql.*; public class TestJDBC { // orcl为oracle数据库中的数据库名,localhost表示连接本机的oracle数据库 // 1521 ...

  10. js 乱码解决方案

    如果网页编码规则是utf-8,即在网页头部中可以看到如下代码: 那么js文件中如果有中文字,调用输出时就会出现乱码.解决此个问题的办法如下: 即在引用javascript输出的地方加上charset= ...