3391: [Usaco2004 Dec]Tree Cutting网络破坏
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 296 Solved: 218
[ 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
水题,DFS一遍,记一下每个点子树大小就行了
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> G[10005];
int n, cnt, ans[10005], bet[10005];
void Sech(int u, int p)
{
int i, v, now;
bet[u] = 1, now = 0;
for(i=0;i<G[u].size();i++)
{
v = G[u][i];
if(v==p)
continue;
Sech(v, u);
now = max(bet[v], now);
bet[u] += bet[v];
}
now = max(now, n-bet[u]);
if(now<=n/2)
ans[++cnt] = u;
}
int main(void)
{
int x, y, i;
scanf("%d", &n);
for(i=1;i<=n-1;i++)
{
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
Sech(1, 0);
if(cnt==0)
printf("NONE\n");
else
{
sort(ans+1, ans+cnt+1);
for(i=1;i<=cnt;i++)
printf("%d\n", ans[i]);
}
return 0;
}