poj 2378 Tree Cutting

Tree Cutting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3258   Accepted: 1914

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

Hint

INPUT DETAILS: 

The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles. 

OUTPUT DETAILS: 

If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).

Source

USACO 2004 December Silver

题目大意:给你一颗树问去掉哪些结点后能使省下来的森林的结点数不超过原数节点数的一半。

#include <iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
struct node1//点结构
{
    int sum;//存子树的总结点数
    int maxs;//儿子结点的最大规模。见我最近写的男人八题
} points[20010];
struct node2//边结构
{
    int to;//终点
    node2 *next;
} edge[20010],*head[10010];
int pos[10010],pmaxs[10010],ans[10010];
int cnt,ptr,nans,n;
void adde(int f,int s)//加边
{
    edge[cnt].next=head[f];
    edge[cnt].to=s;
    head[f]=&edge[cnt++];
}
int ma(int a,int b){ return a>b?a:b; }
void dfs(int fa,int son)//以s为根结点遍历树。计算树的总结点数和子树的最大规模
{
    points[son].maxs=0;
    points[son].sum=1;
    node2 *p=head[son];
    while(p!=NULL)
    {
        if(p->to!=fa)
        {
            dfs(son,p->to);
            points[son].maxs=ma(points[son].maxs,points[p->to].sum);
            points[son].sum+=points[p->to].sum;
        }
        p=p->next;
    }
    pos[ptr]=son;//把算的结果存数组中
    pmaxs[ptr++]=points[son].maxs;
}
void solve(int s)
{
    int i,temp;
    ptr=0;
    dfs(0,s);
    temp=points[s].sum;
    nans=0;
    for(i=0;i<ptr;i++)
    {
        pmaxs[i]=ma(pmaxs[i],temp-points[pos[i]].sum);
        if(pmaxs[i]<=n/2)//把满足条件的结点存在数组中
            ans[nans++]=pos[i];
    }
}
int main()
{
    int i,a,b;

    while(~scanf("%d",&n))
    {
        memset(head,0,sizeof head);
        cnt=0;
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            adde(a,b);
            adde(b,a);
        }
        solve(1);
        if(nans>0)
        {
            sort(ans,ans+nans);//题目要求升序。所以排下序
            for(i=0;i<nans;i++)
                printf("%d\n",ans[i]);
        }
        else
            printf("NONE\n");
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)