URAL 1056 Computer Net (Shortest way)

Source: Internet
Author: User

Computer NetTime limit:2.0 Second
Memory limit:64 mbbackgroundcomputer Net is created by consecutive computer plug-up to one it has already been connecte D to the net. Each new computer gets a ordinal number, but the protocol contains the number of their parent computer in the net. Thus, protocol consists of several numbers; The first of them is all 1, because the second computer can only be connected to the first one, and the second number is 1 or 2 and so forth. The total quantity of numbers in the protocol is N? 1 ( Nis 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 are 2, and between #3 and #5 is 3. Definition.Let the center of the net being the computer which have a minimal distance to the most remote computer. In the shown example computers #1 and #2 is the centers of the net. Problemyour task is to find all the centers using the set protocol. Inputthe first line of input contains an integer N, the quantity of computers (2≤ N≤10000). Successive N? 1 lines contain protocol. Outputoutput should contain ordinal numbers of the determined net centers in ascending order. Sample
input Output
51122
1 2
problem Source:Rybinsk State Avia Academy "Analysis" gives you a tree, n nodes, N-1 edge, next n-1 line, I th line represents the node number connected to I, you need to find a few points, so that these points from the farthest point distance from the smallest. It is the shortest way to find the diameter of the tree, record the length of the diameter, and then find the midpoint along the diameter.
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<stack>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)typedefLong Longll;using namespacestd;Const intN =10005;Const intM =24005;intVis[n],dis[n],pre[n],head[n];intn,m,tot=0, SON,MAXN;structedg{intTo,next;} edg[n*N];voidAddintUintv) {edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;}voidBFsints) {met (Vis,0Met (Dis,inf), met (Pre,0); Dis[s]=0; Vis[s]=1; maxn=0; Queue<int>Q;q.push (s);  while(!Q.empty ()) {        intU=q.front (); Q.pop (); vis[u]=0;  for(inti=head[u];i!=-1; i=Edg[i].next) {            intv=edg[i].to; if(dis[v]>dis[u]+1) {Dis[v]=dis[u]+1; Maxn=max (Maxn,dis[v]);p re[v]=u; if(!Vis[v]) {Q.push (v); Vis[v]=1; }}} son=u; }}intMain () {Met (head,-1); intA[n],cnt=0; scanf ("%d",&N);  for(intI=2; i<=n;i++) {scanf ("%d",&m); add (i,m); add (m,i); } BFS (1);ints=Son; BFS (son);intT=son;//printf ("!! %d%d\n ", s,t);    if(maxn&1){        intx=maxn/2;  while(t) {if(dis[t]==x| | dis[t]==x+1) a[cnt++]=T; T=Pre[t]; }    }    Else {        intx=maxn/2;  while(t) {if(dis[t]==x) a[cnt++]=T; T=Pre[t]; }} sort (A,a+CNT);  for(intI=0; i<cnt;i++) printf ("%d", A[i]); printf ("\ n"); return 0;}

URAL 1056 Computer Net (Shortest way)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.