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. High Performance Django

    构建高性能Django站点   性能 可用 伸缩 扩展 安全 build 1.审慎引入第三方库(是否活跃.是否带入query.是否容易缓存) 2.db:减少query次数 减少耗时query 减小返回 ...

  2. 从协议VersionedProtocol开始1

    Phase 0: Make a plan You must first decide what steps you're going to have in your process. It sound ...

  3. 类似github的框架

    github是程序员经常上的网站,但如果是在一家苦逼不能访问外网的公司,那不能把自己的代码托管在github上绝对是一件非常痛苦的事情.如果想要在公司内网也可以用github托管自己的代码,那就要自己 ...

  4. shell指令expr和test指令

    通过expr指令可以进行+.-.*.\.%等运算,但是有一点值得注意,使用乘法时,要在*前加上一个\符号. 通过test指令可以进行逻辑测试,进行测试的情况有四种: 1.整数测试 a.判断两个整数是否 ...

  5. .net 小技巧

    简单提示效果: <input runat="server" type="text" id="SelPerson" value=&quo ...

  6. InterruptedException 线程异常

    InterruptedException 这个异常一般发生在线程中,当一个正在执行的线程被中断时就会出现这个异常-! 简单的说就是:假如有两个线程,第一个线程正在运行,第二个没有运行,这时第二个线程启 ...

  7. Microsoft Visual Studio Ultimate 2013 with Update 3 CN+EN

    官方90天试用版. Microsoft Visual Studio Ultimate 2013 with Update 3 - 简体中文DVD5 ISO image (SHA-1: 9A306631A ...

  8. iOS 引导页组件 HcdGuideView

    HcdGuideView HcdGuideView让你为你的app添加一个漂亮的启动页变得简单. 要求 Xcode 6 or higher iOS 7.0 or higher ARC 安装方法 手动安 ...

  9. 如何使用SVN管理我们的源代码

    今天把公司的SVN服务器配置给做了一下,根据公司部门的不同,划分了不同的访问目录,并给不同目录配置了相应的权限,算是把这份差事给干完了,但其实我对自己的工作是不满意和有遗憾的,因为目前公司的SVN服务 ...

  10. 理解smart pointer之三:unique_ptr

    unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一. std::unique_ptr is a smart pointer that retain ...