POJ 2378.1655.3107 (树形DP 删点类树形DP)
来源:互联网 发布:淘宝 金肉山 这么便宜 编辑:程序博客网 时间:2024/05/29 09:54
Description
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
* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.
Output
Sample Input
101 22 33 44 56 77 88 99 103 8
Sample Output
38
Hint
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).
Description
For example, consider the tree:
Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.
For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
Input
Output
Sample Input
172 61 21 44 53 73 1
Sample Output
1 2
Description
Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.
Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.
Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.
Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.
Input
The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.
The following n − 1 lines contain two integer numbers each. The pair ai, bi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.
Output
Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.
Sample Input
61 22 32 53 43 6
Sample Output
2 3
#include <stdio.h>#include <string.h>#include <algorithm>#define N 10010using namespace std;int head[N],etot,tot,n,ans[N];struct edge{int v,next;}g[500010];void add_edge(int u,int v){g[etot].v=v;//g[etot].v是etot这条边指向的点g[etot].next=head[u];//g[etot].next是etot这条边的前一条边的标号(即同一个节点出去的其他分支)head[u]=etot++;//head[u]是u这个点出去的边的标号。}int dfs(int u,int fa){int child_max=-1,sum=1;//要加上自己,所以sum初始化为1for(int i=head[u];i!=-1;i=g[i].next)//i=-1,表示从这点出发的所有分支都搜过了。可以结束循环。{int v=g[i].v;if(v==fa)//v==fa表示是来时的那条分支。不走。continue;int value=dfs(v,u);//往下搜。即搜v这个点,v这个点的来时的点是uif(value>child_max)child_max=value;//更新sum+=value;}if(n-sum>child_max)child_max=n-sum;if(child_max<=n/2)ans[tot++]=u;return sum;}int main(){while(scanf("%d",&n)!=EOF){etot=0;tot=0;memset(head,-1,sizeof(head));//head初始化为-1 表示前面么有了for(int i=0;i<n-1;i++){int a,b;scanf("%d%d",&a,&b);add_edge(a,b);add_edge(b,a);//加双向边}dfs(1,-1);//从1节点开始,将1节点的前一个节点设为-1if(tot==0)printf("NONE\n");else{sort(ans,ans+tot);for(int i=0;i<tot;i++){printf("%d\n",ans[i]);}}}return 0;}
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define N 20010int head[N],tot,etot,n;struct edge{int v,next;}g[500010];struct minn{int point,value,time;}mins;void add_edge(int u,int v){g[etot].v=v;g[etot].next=head[u];head[u]=etot++;}int dfs(int u,int fa){int child_max=-1,sum=1;for(int i=head[u];i!=-1;i=g[i].next){int v=g[i].v;if(v==fa)continue;int value=dfs(v,u);if(value>child_max)child_max=value;sum+=value;}if(n-sum>child_max)child_max=n-sum;if(child_max<mins.value){mins.value=child_max;mins.point=u;mins.time=1;}else if(child_max==mins.value){mins.time++;if(mins.point>u)mins.point=u;}return sum;}int main(){int t;while(scanf("%d",&t)!=EOF){while(t--){memset(head,-1,sizeof(head));mins.value=200000;scanf("%d",&n);for(int i=0;i<n-1;i++){int a,b;scanf("%d%d",&a,&b);add_edge(a,b);add_edge(b,a);}dfs(1,-1);printf("%d %d\n",mins.point,mins.value);}}return 0;}
#include <stdio.h>#include <string.h>#define N 50010#include <algorithm>using namespace std;int head[N],etot,tot,n,ans[N];struct edge{int v,next;}g[500010];void add_edge(int u,int v){g[etot].v=v;g[etot].next=head[u];head[u]=etot++;}struct minn{int value;int point[N];}mins;int dfs(int u,int fa){int child_max=-1,sum=1;for(int i=head[u];i!=-1;i=g[i].next){int v=g[i].v;if(v==fa)continue;int value=dfs(v,u);if(value>child_max)child_max=value;sum+=value;}if(n-sum>child_max)child_max=n-sum;if(child_max<mins.value){mins.value=child_max;//memset(ans,0,sizeof(ans));tot=0;ans[tot++]=u;}else if(child_max==mins.value){ans[tot++]=u;}return sum;}int main(){while(scanf("%d",&n)!=EOF){memset(head,-1,sizeof(head));etot=0;mins.value=500000;for(int i=0;i<n-1;i++){int a,b;scanf("%d%d",&a,&b);add_edge(a,b);add_edge(b,a);}dfs(1,-1);sort(ans,ans+tot);//printf("%d\n",tot);for(int i=0;i<tot;i++){if(i==tot-1)printf("%d\n",ans[i]);elseprintf("%d ",ans[i]);}}return 0;}
- POJ 2378.1655.3107 (树形DP 删点类树形DP)
- poj 3107 树形dp
- POJ 3107 树形dp
- poj 3107(树形dp)
- POJ 3107 Godfather(树形dp)
- Godfather - POJ 3107 树形dp
- POJ 3107 Godfather (树形DP)
- poj 3107 Godfather (树形dp)
- 【POJ】3107 Godfather 树形dp
- poj 1770 树形dp
- poj 3342 树形DP
- poj 2486 树形dp
- poj 2486 树形DP
- 【树形dp】POJ 1463
- poj 2486 树形dp
- poj 3140 树形dp
- POJ 2152(树形DP)
- poj 1947 树形dp
- 中小企业如何做好营销团队管理
- python dict 数据
- JSP内置对象比较与分析(http://mynote.iteye.com/blog/1103102)
- 布尔型变量的初始化问题
- POJ 2155——Matrix(树套树,二维树状数组,二维线段树)
- POJ 2378.1655.3107 (树形DP 删点类树形DP)
- Phonegap插件开发攻略
- WebView自定义背景
- 25个增强iOS应用程序性能的提示和技巧(中级篇)
- JAVA 获取 FULL url
- poj1847 - Tram (最短路径问题) (Dijkstra)
- 骁龙805支持4K显示 内存带宽翻倍是亮点
- ORA-24247: 网络访问被访问控制列表 (ACL) 拒绝
- 什么是字符串常量池