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 - 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

input output
5
1
1
2
2
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)的更多相关文章

  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. java --对象流与对象的序列化

    对象流 ObjectInputStream ObjectOutputStream类分别是InputStream和OutputStream的子类,对象输出流使用writeObject(Object ob ...

  2. android 图片尺寸 资料

  3. (SQL SERVER) (ORACLE) (ACCESS)(POSTGRE SQL)四种数据库操作C#代码

    将对这四种数据库的操作封装到了2个类中可以拷贝过去直接使用. public sealed class OleDbClass { #region private utility methods & ...

  4. Boost::filesystem 使用小笔记

    今天拿起手要用C++写个小工具,从指定的目录递归遍历文件,然后做一下处理.又翻了一下boost的filesystem库.小结一下,希望能加深印象,免得下次又要查看文档. 1. path对象就是一个跨平 ...

  5. poj 3767 I Wanna Go Home

    题意:n个点(从1-n编号) m条边 下面m行 u v dis 表示双向边u v的距离 n个点表示 每个点被势力1或2占据 这里保证1 城市由势力1占据,2城市由势力2占据 思路: 求2遍spfa() ...

  6. javascript每日一练(四)——DOM二

    一.DOM的创建,插入,删除 createElement(标签名) appendChild(节点) insertBefore(节点,原有节点) removeChild(节点) <!doctype ...

  7. javascript笔记整理(函数)

    javascript函数的声明和调用将完成某一特定功能的代码集合起来,可以重复使用的代码块. 一.函数的声明方式(创建) A.基本语法(function  关键字)function 函数名([参数1] ...

  8. 读取中兴3G告警log告警文件到集合

    1.文件格式 ALARM_ID=102305_404205 EVENT_TIME=-- :: NOTIFICATION_TYPE= MANAGED_OBJECT_INSTANCE=NodeId=,Bs ...

  9. OpenRisc-35-基于orpsoc,eCos的sd card controller的测试实验

    引言 之前,曾经在orpsoc的平台上,测试验证过其sd card controller的linux的驱动,但是并不是很完美,经过努力,终于在eCos下完成了其全部功能的验证,包括驱动层验证,文件系统 ...

  10. jquery 获取 TABLE单元格的值

    1.JQ部分: var tds = $("#table1 td"); tds.click(function(){ //给所有td添加点击事件        var tdSeq = ...