因为是棵树 , 所以直接 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. ButterKnife 绑定 RadioGroup

    原则上 ButterKnife 是不支持 RadioGroup 的, 可以通过以下方法添加RadioButton的点击事件: @OnClick({ R.id.radio_btn1, R.id.radi ...

  2. 转移服务器,DEDE网站遇到no input file specified!

    公司新配置了服务器,需要从旧服务器上把原来的站点迁移到新服务器,迁移.NET网站没有遇到任何问题,但是在迁移过来一个用DEDE做的网站后,访问首页出现No Input File Specified,后 ...

  3. jmeter cookie管理器 使用方法---新手学习记录1

    首先得抓包: 我已post方法为例: POST /api/datasources/lemontest/jaql HTTP/1.1 Host: 192.168.1.107:8081 Content-Le ...

  4. C和指针 读书笔记

    准备复习一下之前读过的<C和指针>,主要看之前标记过的地方. 感觉像第一次看的地方再记录一下-- 1.预处理器读入源代码,根据预处理指令对其进行修改,然后将修改后的源代码交给编译器. 2. ...

  5. EventStore的设计思路

    EventStore的设计思路 最近打算用C#实现一个基于文件的EventStore. 什么是EventStore 关于什么是EventStore,如果还不清楚的朋友可以去了解下CQRS/Event ...

  6. 安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机

    版权声明:本文为博主原创文章,未经博主允许不得转载. 我们安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机,我们可以通过VMware vSphere Client直接 ...

  7. 在 Windows Media Center 中观看电视

    如果计算机具备了必要的硬件,则可以在电脑上使用 Windows Media Center 观看.暂停和快退直播的电视节目及录制的电视节目. 通过 Windows Media Center 观看直播电视 ...

  8. qwebkit - Open new window after click using QT - Stack Overflow

    qwebkit - Open new window after click using QT - Stack Overflow Open new window after click using QT

  9. HDU 4716 A Computer Graphics Problem

    A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  10. 面试之hack(转载)

    史上最全的CSS hack方式一览 css hack api   做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方 ...