POJ2378 - Tree Cutting

Description

After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.

Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.

Please help Bessie determine all of the barns that would be suitable to disconnect.

Input

* Line 1: A single integer, N. The barns are numbered 1..N.

* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.

Output

* Lines 1..?: Each line contains a single integer, the number (from 1..N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word "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

题意:

给定一棵无根树,输出删除某一结点能得到一个连通度为k的树的结点编号

分析:

对于任意一棵子树,求出其最大子树的连通度,若其(最大子树连通度<=N/2)并且(N-当前子树的总连通度<=N/2),则说明该结点符合要求。

代码:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
#define INF 0xffffff
using namespace std;

int N;
int x, y;
vector<int> Tree[10005];
vector<int> Vis(10005);
vector<int> Children(10005);
vector<int> V;

int DFS(int n) {
    int bigger = 0;
    for (int i = 0; i < Tree[n].size(); i++)
        if (!Vis[Tree[n][i]]) {
            Vis[Tree[n][i]] = 1;
            int tmp = DFS(Tree[n][i]);
            Children[n] += tmp;
            if (bigger < tmp)
                bigger = tmp;
        }
    if (bigger <= N / 2 && (N - Children[n]) <= N / 2)
        V.push_back(n);
    return Children[n];
}
int main()
{
    cin >> N;
    for (int i = 1; i < N; i++) {
        cin >> x >> y;
        Tree[x].push_back(y);
        Tree[y].push_back(x);
    }
    Vis.assign(10005, 0);
    Children.assign(10005, 1);
    Vis[1] = 1;
    DFS(1);
    sort(V.begin(), V.end());
    if (!V.size())
        cout << "NONE" << endl;
    for (int i = 0; i < V.size(); i++)
        cout << V[i] << endl;
    return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值