poj 2378 Tree Cutting
来源:互联网 发布:中国新歌声2网络直播 编辑:程序博客网 时间:2024/06/08 06:53
Tree Cutting
Time Limit: 1000MS Memory Limit: 65536KTotal 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.
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.
* 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
101 22 33 44 56 77 88 99 103 8
Sample Output
38
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).
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;}
- poj 2378 Tree Cutting
- POJ 2378 Tree Cutting
- POJ 2378 Tree Cutting
- poj 2378 Tree Cutting
- POJ 2378 Tree Cutting
- poj 2378 Tree Cutting
- poj 2378 Tree Cutting
- [POJ 2378] Tree Cutting
- POJ-2378-Tree cutting
- POJ 2378 Tree Cutting 1655 Blancing Act
- POJ--2378--Tree Cutting--树形DP
- poj 2378 Tree Cutting(树形dp)
- (简单) 树形dp POJ 2378 Tree Cutting
- poj 2378 Tree Cutting (树形dp)
- poj 2378 Tree Cutting 树形dp
- Tree Cutting - POJ 2378 树形dp
- POJ 2378 Tree Cutting 子树统计
- POJ 2378 Tree Cutting(树形DP)
- C# RichTextbox的字体格式和颜色
- 前段弹窗实现方法
- 线性结构 循环队列
- Tomcat 第四讲
- 各种常见排序算法的比较
- poj 2378 Tree Cutting
- Android的Adapter的用法
- 大白话解析模拟退火算法
- linux进入图形界面的方法
- SAP 设置或取消仓库不参与MRP运算
- Ubuntu 13.04 使用 goagent + chrome 体验自由网络
- easyui datagrid 日间格化(JS 日期时间本地化显示)
- C++ Primer 中文版 学习笔记(五)
- JAVA等级对比表