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
Problem Source: Rybinsk State Avia Academy
【分析】给你一棵树,n个节点,n-1条边,接下来n-1行,i th行表示与i相连的节点编号,要你找几个点,使得这几个点离最远的那个点的距离最小。
 做法就是最短路找这棵树的直径,记录直径的长度,然后顺着这条直径找中点。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int vis[N],dis[N],pre[N],head[N];
int n,m,tot=,son,maxn;
struct EDG{int to,next;}edg[N*N];
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void bfs(int s) {
met(vis,);met(dis,inf);met(pre,);
dis[s] = ;
vis[s] = ;maxn=;
queue<int>q;q.push(s);
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(dis[v]>dis[u]+){
dis[v]=dis[u]+;maxn=max(maxn,dis[v]);pre[v]=u;
if(!vis[v]){
q.push(v);vis[v]=;
}
}
}
son=u;
}
}
int main() {
met(head,-);
int a[N],cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m);add(i,m);add(m,i);
}
bfs();int s=son;
bfs(son);int t=son;//printf("!!%d %d\n",s,t);
if(maxn&){
int x=maxn/;
while(t){
if(dis[t]==x||dis[t]==x+)a[cnt++]=t;
t=pre[t];
}
}
else {
int x=maxn/;
while(t){
if(dis[t]==x)a[cnt++]=t;
t=pre[t];
}
}
sort(a,a+cnt);
for(int i=;i<cnt;i++)printf("%d ",a[i]);
printf("\n");
return ;
}

URAL 1056 Computer Net(最短路)的更多相关文章

  1. URAL 1056(树形DP)

    1056. Computer Net Time limit: 2.0 second Memory limit: 64 MB Background Computer net is created by ...

  2. URAL 1085 Meeting(最短路)

    Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...

  3. URAL 1934 Black Spot(最短路)

    Black Spot Time limit: 1.0 secondMemory limit: 64 MB Bootstrap: Jones's terrible leviathan will find ...

  4. URAL 2069 Hard Rock (最短路)

    题意:给定 n + m 个街道,问你从左上角走到右下角的所有路的权值最小的中的最大的. 析:我们只要考虑几种情况就好了,先走行再走列和先走列再走行差不多.要么是先横着,再竖着,要么是先横再竖再横,要么 ...

  5. Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)

    1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...

  6. URAL 2034 Caravans(变态最短路)

    Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...

  7. Let‘s play computer game(最短路 + dfs找出所有确定长度的最短路)

    Let's play computer game Description xxxxxxxxx在疫情期间迷上了一款游戏,这个游戏一共有nnn个地点(编号为1--n1--n1--n),他每次从一个地点移动 ...

  8. DP/最短路 URAL 1741 Communication Fiend

    题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...

  9. URAL 1002 Phone Numbers(KMP+最短路orDP)

    In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...

随机推荐

  1. Problem C HDU 5224

    Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows t ...

  2. CCNA 6.9

    page 201 show ip route    Correction(05-4)   Basic configuration of R1:   enable configure terminal ...

  3. 项目管理软件kanboard安装

    1. php环境 2. php扩展

  4. JS创建自定义对象

    普通对象的创建: 创建对象: 1.people = new Object(); people.name = "lin"; people.age = "26“; 2.创建字 ...

  5. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

  6. Interview----2 sum

    题目:输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字. 要求时间复杂度是 O(n).如果有多对数字的和等于输入的数字,输出任意一对即可. 例如输入数组 ...

  7. Makefile总结和反序字符数组,整体右移数组,杨辉三角!

    2015.1.23今天我值日 继续接着昨天Makefile的目标开始记录:第一种:有 .PHNOY声明,但是冒号后面没有依赖文件.PHNOY:clean clean://没有依赖文件 rm *.0 t ...

  8. iOS常用正则表达式验证(手机号、密码格式、身份证号等)

    #import @interfaceUtils : NSObject #pragma 正则匹配手机号 + (BOOL)checkTelNumber:(NSString*) telNumber; #pr ...

  9. Median Weight Bead_floyd

    Description There are N beads which of the same shape and size, but with different weights. N is an ...

  10. 30道四则运算<2>单元测试

    该测试未实现除法 该测试中间多了/)两个符号,而且没有等号和回车. 该测试也没有符合除法要求 该测试也没有满足除法要求 该测试满足要求. 总结:程序中涉及到有除法的输出都有问题,多次改正未果:其他条件 ...