因为是棵树 , 所以直接 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. HDU2007-平方和与立方和

    描述: 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和. 代码: #include<stdio.h> #include<string.h> #include ...

  2. Java中单态设计模式

    Java中单态设计模式 2011-09-23 16:38:46|  分类: Java |  标签:technology!  |举报|字号 订阅     此博文是转自新浪博客中一名叫做"俊俊的 ...

  3. php Smarty详细配置

    1.在Smarty官网下载 路径:https://github.com/smarty-php/smarty/releases 2.把下载下来的Smarty解压出来 3.把解压出来的Smarty里面的l ...

  4. 射频识别技术漫谈(16)——Mifare UltraLight

    Mifare UltraLight又称为MF0,从UltraLight(超轻的)这个名字就可以看出来,它是一个低成本.小容量的卡片.低成本,是指它是目前市场中价格最低的遵守ISO14443A协议的芯片 ...

  5. 10003 Cutting Sticks(区间dp)

      Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog ...

  6. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  7. Sass入门——简介+语法格式及编译调试

    本文来自慕课网大漠. Sass简介 Sass和SCSS区别 1.后缀名不同,很好理解 2.Sass以严格的缩进语法规则书写,不带大括号和分号:而SCSS的语法规则和CSS的语法很类似. Sass: $ ...

  8. 在 Windows系统中编译node.js 源代码

    Node.js 在 Windows 下只能通过 Microsoft Visual Studio 编译,因此你需要首先安装 Visual Studio 或者免费的 Visual Studio Expre ...

  9. opencv第一站:配置opencv环境(2015-12-12)

    今天论坛申请的书< OpenCV 计算机视觉编程攻略(中国工信出版社)>到了,准备研究研究机器视觉. 晚上安装了 vc2008 及 opencv 最新版 3.0.0,试了各种配置都是错误提 ...

  10. ajax.js

    /**通用ajax服务的定义对象 * services可以是单个服务对象,也可以是service服务数组 * 具体服务的定义请参考appendServices成员函数 */ function Serv ...