JZOJ 1301. treecut

1301. treecut (Standard IO)

Time Limits: 1000 ms Memory Limits: 131072 KB

Description

  有一个N个节点的无根树,各节点编号为1..N,现在要求你删除其中的一个点,使分割开的连通块中节点个数都不超过原来的一半多。

Input

  第一行:一个整数N (1 <= N <= 10,000)。   后面有N-1行:每行两个整数 X 和 Y,表示一个边连接的两个节点号。

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

Data Constraint

Hint

样例说明:   删除3号或8号节点,则分枝最多有5个节点

题解

人为规定1为根
然后dfs搜索就可以了

每次求出节点i的所有儿子的儿子数
然后判断是否满足小于等于n/2

代码

#include<cstdio>
#include<algorithm>
#include<vector>
#define N 10001
using namespace std;

vector<long>map[N];

long n;
long lc[N],rb[N],ans[N];
bool b[N];

void build(long now)
{   long i,next;
    b[now]=true;
    for(i=0;i<map[now].size();i++)if(!b[map[now][i]]){
        if(!lc[now])lc[now]=map[now][i];
        else rb[next]=map[now][i];
        next=map[now][i];
        build(next);
    }
}
long dfs(long now)
{   long i,q,num=0;
    bool t=true;
    for(i=lc[now];i;i=rb[i]){
        if((q=dfs(i))-1>=long(double(n)/2+0.9))
            t=false;
        num+=q;
    }
    if(t&&n-(num+1)<=long(double(n)/2+0.9))ans[++ans[0]]=now;
    return num+1;
}

int main()
{   long i,x,y;
    scanf("%ld",&n);
    for(i=1;i<n;i++){
        scanf("%ld%ld",&x,&y);
        map[x].push_back(y);
        map[y].push_back(x);
    }
    build(1);
    dfs(1);
    if(!ans[0])printf("NONE\n");
    else{
        sort(ans+1,ans+ans[0]+1);
        for(i=1;i<=ans[0];i++)
            printf("%ld\n",ans[i]);
    }
    return 0;
}
posted @ 2017-07-09 21:51  dreaming2019  阅读(105)  评论(0)    收藏  举报