URAL 1056(树形DP)
来源:互联网 发布:java jdk1.8 32位 编辑:程序博客网 时间:2024/06/06 23:58
1056. Computer Net
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Background
Computer net is created by consecutive computer plug-up to one that has already been connected to the net. Each new computer gets an ordinal number, but the protocol contains the number of its parent computer in the net. Thus, protocol consists of several numbers; the first of them is always 1, because the second computer can only be connected to the first one, the second number is 1 or 2 and so forth. The total quantity of numbers in the protocol is N − 1 (N is a total number of computers). For instance, protocol 1, 1, 2, 2 corresponds to the following net:
1 - 2 - 5| |3 4
The distance between the computers is the quantity of mutual connections (between each other) in chain. Thus, in example mentioned above the distance between computers #4 and #5 is 2, and between #3 and #5 is 3.
Definition. Let the center of the net be the computer which has a minimal distance to the most remote computer. In the shown example computers #1 and #2 are the centers of the net.
Problem
Your task is to find all the centers using the set protocol.
Input
The first line of input contains an integer N, the quantity of computers (2 ≤ N ≤ 10000). Successive N − 1 lines contain protocol.
Output
Output should contain ordinal numbers of the determined net centers in ascending order.
Sample
51122
1 2
题意:1号电脑为根节点,给出2~n号电脑的父节点,求出用那些电脑当中心,到其余电脑的最大距离最小。
思路:一点到所有子节点的最大距离很好求,但是我们还要知道通过父节点所能到达的最大距离,所以先处理一下,求出所有点到其子节点的最大距离边和次大距离边,然后再分析每个节点是否在其父节点的最大距离边上,如果在的话,就用父节点的次大距离来更新该节点,否则用父节点的最大距离来更新。
#include<stdio.h>#include<string.h>const int N=10001;int dp[N][2],vis[N],head[N],num,ans;struct edge{int st,ed,next;}e[N*4];void addedge(int x,int y){e[num].st=x;e[num].ed=y;e[num].next=head[x];head[x]=num++;e[num].st=y;e[num].ed=x;e[num].next=head[y];head[y]=num++;}void dfs1(int u){vis[u]=1;int i,v;for(i=head[u];i!=-1;i=e[i].next){v=e[i].ed;if(vis[v]==1)continue;dfs1(v);if(dp[v][1]+1>dp[u][1]){dp[u][0]=dp[u][1];dp[u][1]=dp[v][1]+1;}else if(dp[v][1]+1>dp[u][0])dp[u][0]=dp[v][1]+1;}}void dfs2(int u){vis[u]=1;int i,v,temp;for(i=head[u];i!=-1;i=e[i].next){v=e[i].ed;if(vis[v]==1)continue;if(dp[u][1]==dp[v][1]+1)//在父节点的最大距离边上temp=dp[u][0]+1;else temp=dp[u][1]+1;if(temp>dp[v][1]) //更新v节点的最大,次大边 { dp[v][0]=dp[v][1]; dp[v][1]=temp; } else if(temp>dp[v][0]) { dp[v][0]=temp; } if(ans>dp[v][1])ans=dp[v][1];dfs2(v);}}int main(){int n,i,x;while(scanf("%d",&n)!=-1){memset(head,-1,sizeof(head));memset(dp,0,sizeof(dp));num=0;for(i=2;i<=n;i++){scanf("%d",&x);addedge(i,x);}ans=99999999;memset(vis,0,sizeof(vis));dfs1(1);memset(vis,0,sizeof(vis));dfs2(1);if(ans>dp[1][1])ans=dp[1][1];for(i=1;i<=n;i++){if(dp[i][1]==ans)printf("%d ",i);}printf("\n");}return 0;}
- URAL 1056(树形DP)
- ural 1018(树形dp)
- URAL - 1018 (树形dp 水题)
- Ural 1018 Binary Apple Tree 树形dp
- 第一道树形dp ural 1018
- URAL 1018 (金典树形DP)
- URAL 1039 Anniversary Party 树形dp
- URAL 1039 Anniversary Party (树形DP)
- URAL 1018 Binary Apple Tree (树形DP)
- URAL 1018 Binary Apple Tree (树形DP)
- URAL 1039 / poj2342-Anniversary Party-树形DP
- Ural 1018 Binary Apple Tree [树形dp]
- ural 1056 Computer Net(树形DP)需要用到两遍dfs
- Ural 1018 Binary Apple Tree (树形DP)
- Ural 1018 Binary Apple Tree(树形dp)
- timus 1039. Anniversary Party URAL解题报告 树形DP
- ural 1018 Binary Apple Tree(树形dp | 经典)
- URAL 1018 Binary Apple Tree(树形dp入门题)
- 普林斯顿大学算法课程第二周个人总结
- 用 Firebug 动态调试和优化应用程序 .
- C++模板高级用法
- perl连接数据库的写法
- 用firebreath开发插件
- URAL 1056(树形DP)
- 《算法竞赛-训练指南》-第三章-Trie
- NuGet学习笔记(2)——使用图形化界面打包自己的类库
- ZOJ 3689
- .net wcf 返回DataTable 调用 ”基础连接已经关闭: 连接被意外关闭“
- 交换两个元素
- C#Ldap查找用户信息
- FFmpeg 移植到 Android 平台
- iOS 实现杨辉三角