1056. Computer Net

Time limit: 2.0 second
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. 1 - 2 - 5
  2. | |
  3. 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

input output
  1. 5
  2. 1
  3. 1
  4. 2
  5. 2
  1. 1 2

题意:1号电脑为根节点,给出2~n号电脑的父节点,求出用那些电脑当中心,到其余电脑的最大距离最小。
思路:一点到所有子节点的最大距离很好求,但是我们还要知道通过父节点所能到达的最大距离,所以先处理一下,求出所有点到其子节点的最大距离边和次大距离边,然后再分析每个节点是否在其父节点的最大距离边上,如果在的话,就用父节点的次大距离来更新该节点,否则用父节点的最大距离来更新。



  1. #include<stdio.h>
  2. #include<string.h>
  3. const int N=10001;
  4. int dp[N][2],vis[N],head[N],num,ans;
  5. struct edge
  6. {
  7. int st,ed,next;
  8. }e[N*4];
  9. void addedge(int x,int y)
  10. {
  11. e[num].st=x;e[num].ed=y;e[num].next=head[x];head[x]=num++;
  12. e[num].st=y;e[num].ed=x;e[num].next=head[y];head[y]=num++;
  13. }
  14. void dfs1(int u)
  15. {
  16. vis[u]=1;
  17. int i,v;
  18. for(i=head[u];i!=-1;i=e[i].next)
  19. {
  20. v=e[i].ed;
  21. if(vis[v]==1)continue;
  22. dfs1(v);
  23. if(dp[v][1]+1>dp[u][1])
  24. {
  25. dp[u][0]=dp[u][1];
  26. dp[u][1]=dp[v][1]+1;
  27. }
  28. else if(dp[v][1]+1>dp[u][0])
  29. dp[u][0]=dp[v][1]+1;
  30. }
  31. }
  32. void dfs2(int u)
  33. {
  34. vis[u]=1;
  35. int i,v,temp;
  36. for(i=head[u];i!=-1;i=e[i].next)
  37. {
  38. v=e[i].ed;
  39. if(vis[v]==1)continue;
  40. if(dp[u][1]==dp[v][1]+1)//在父节点的最大距离边上
  41. temp=dp[u][0]+1;
  42. else temp=dp[u][1]+1;
  43. if(temp>dp[v][1]) //更新v节点的最大,次大边
  44. {
  45. dp[v][0]=dp[v][1];
  46. dp[v][1]=temp;
  47. }
  48. else if(temp>dp[v][0])
  49. {
  50. dp[v][0]=temp;
  51. }
  52. if(ans>dp[v][1])
  53. ans=dp[v][1];
  54. dfs2(v);
  55. }
  56. }
  57. int main()
  58. {
  59. int n,i,x;
  60. while(scanf("%d",&n)!=-1)
  61. {
  62. memset(head,-1,sizeof(head));
  63. memset(dp,0,sizeof(dp));
  64. num=0;
  65. for(i=2;i<=n;i++)
  66. {
  67. scanf("%d",&x);
  68. addedge(i,x);
  69. }
  70. ans=99999999;
  71. memset(vis,0,sizeof(vis));
  72. dfs1(1);
  73. memset(vis,0,sizeof(vis));
  74. dfs2(1);
  75. if(ans>dp[1][1])ans=dp[1][1];
  76. for(i=1;i<=n;i++)
  77. {
  78. if(dp[i][1]==ans)
  79. printf("%d ",i);
  80. }
  81. printf("\n");
  82. }
  83. return 0;
  84. }

URAL 1056(树形DP)的更多相关文章

  1. ural 1018(树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 思路:典型的树形dp,处理的时候类似于分组背包,dp[i] ...

  2. Ural 1018 (树形DP+背包+优化)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题目大意:树枝上间连接着一坨坨苹果(不要在意'坨'),给 ...

  3. 树形DP URAL 1039 Anniversary Party

    题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...

  4. ural 1018 Binary Apple Tree(树形dp | 经典)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  5. URAL 1018 (金典树形DP)

    连接:1018. Binary Apple Tree Time limit: 1.0 second Memory limit: 64 MB Let's imagine how apple tree l ...

  6. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  7. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  8. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

  9. hdu1520 第一道树形DP,激动哇咔咔!

    A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. 京东金融集团BD部门招聘 BD经理

    新标签页http://74.55.154.136/ 互联网招聘_cnBeta.COM 北京 / 全职 / 20k-30k / 经验3-5年 / 本科及以上 / 1天前发布 职位诱惑 : 五险一金 职位 ...

  2. Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change

    今天布置环境的时候发现一个问题:Your working copy is out of date. Try pulling from the remote to get the latest chan ...

  3. javascript操作元素的css样式

    我们经常要使用Javascript来改变页面元素的样式.当中一种办法是改变页面元素的CSS类(Class),这在传统的Javascript里,我们一般是通过处理HTML Dom的classname特性 ...

  4. 安卓面试精华(Activity部分)

    过几天小弟要去面试了,当然免不了要好好复习下功课,其实很多东西也不是特别清楚,今天都当作一个回顾和巩固,希望我的这篇文章能对即将去找工作的同学有所帮助. 1. Q:什么是activity? 虽然这个问 ...

  5. IOS7上呈现IOS6的水滴刷新效果

    IOS7上呈现IOS6的水滴刷新效果    到了IOS7 发现自带的刷新 不再是 IOS6自带的水滴效果了 你是否怀念那IOS6的效果呢? 哈哈,于是收集各方资料,整理编写一个属于自己的水滴刷新效果 ...

  6. Python中使用Flask、MongoDB搭建简易图片服务器

    主要介绍了Python中使用Flask.MongoDB搭建简易图片服务器,本文是一个详细完整的教程,需要的朋友可以参考下 1.前期准备 通过 pip 或 easy_install 安装了 pymong ...

  7. 无限层级且乱序的树形结构数据的整理,利用HashMap降低遍历次数

    我们在展示一个机构树的时候,经常会遇到这种一个问题,查询数据的时候,是从下往上查的,但展示数据的时候,又要从下往上展示. 这时候就要把查询到的数据进行整理从而得到我们想要的结构. 举个样例. ID P ...

  8. 【模式识别】Boosting

    Boosting简单介绍 分类中通常使用将多个弱分类器组合成强分类器进行分类的方法,统称为集成分类方法(Ensemble Method).比較简单的如在Boosting之前出现Bagging的方法,首 ...

  9. webform中几个常用的控件

    一,简单控件 1,Lable——标签:在网页中呈现出来的时候会变成span标签 属性:Text——标签上的文字  BackColor,ForeColor——背景色,前景色 Font——字体 Bold- ...

  10. 实现长按删除QListWidget的Item

    原地址:http://blog.sina.com.cn/s/blog_5c70dfc80100r99u.html 要想长按删除QListWidget的Item,必须重写鼠标事件,所以需要继承QList ...