Hdoj 2196 Computer "the diameter of the tree asks for all the longest path starting from any node"

Source: Internet
Author: User

Computer

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4440 Accepted Submission (s): 2236


Problem Descriptiona School bought the first computer some time ago (so this computer ' s ID is 1). During The recent years the school bought N-1 new computers. Each new computer is connected to one of settled earlier. Managers of school is anxious about slow functioning of the net and want to know the maximum distance Si for which i-th C Omputer needs to send signal (i.e. length of cable-the most distant computer). You need to provide this information.


hint:the example input is corresponding to this graph. And from the graph, you can see that the computer 4 are farthest one from 1, so S1 = 3. Computer 4 and 5 is the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. We also get S4 = 4, S5 = 4.

Inputinput file contains multiple test cases. In each case there was natural number N (n<=10000) in the first line, followed by (N-1) lines with descriptions of Compu Ters. I-th line contains-natural numbers-number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input is separated by a space.

Outputfor each case output N lines. I-th line must contain number Si for i-th computer (1<=i<=n).

Sample Input51 12 13 11 1

Sample Output32344 has just begun to use violence, on every node to BFS a predictable LTE test instructions: Buy a computer, every new computer to connect it to the purchase of the last computer   ask all the nodes as the starting point of the longest path,        The given test data means that: Two number A, b means that a computer is connected to the I+1 computer, the required cable length is B (I refers to line i)         2 in the test data 1 means that 2 and 3 are connected with a weighted value of 1.  3 1 means that computer 3 is connected to the computer 4 with a weight of 1; Solving the problem: using three BFs to find the longest road two endpoints U and v because each new computer is connected to the previous computer, each computer's         connection is not forked, That is, the longest road of the node is either the distance to the endpoint u or the distance to the endpoint V (that is, take the larger one), first two times         BFS to find an endpoint record the distance from each point to this endpoint, and then use the BFS to find another endpoint, Then record the distance from each point to this endpoint         finally traverse each point to find the longest
#include <stdio.h> #include <string.h> #include <queue> #define MAX 40100#define maxn (x, y) (x>y?x:y ) using namespace Std;int head[max];int vis[max],dis[max];int n,m,ans,ant;int sum,beg,en;int a[max],b[max];struct node{ int U,v,w;int Next; edge[max];void Add (int u,int v,int W) {Edge[ans].u=u;edge[ans].v=v;edge[ans].w=w;edge[ans].next=head[u];head[u]=ans ++;} void Getmap () {int I,j,a,b;ans=0;memset (head,-1,sizeof (head)), for (i=2;i<=n;i++) {scanf ("%d%d", &a,&b); Add (a,i,b); add (i,a,b);}} void BFs (int sx) {int I,j;queue<int>q;sum=0;memset (vis,0,sizeof (Vis)); memset (dis,0,sizeof (dis)); vis[sx]=1; Beg=sx;q.push (SX), while (!q.empty ()) {int Top=q.front (), Q.pop (), for (i=head[top];i!=-1;i=edge[i].next) {int k=edge[i ].v;if (!vis[k]) {Vis[k]=1;dis[k]=dis[top]+edge[i].w;q.push (k);} if (Sum<dis[k]) {sum=dis[k];beg=k;}}}} void Solve () {int I,j;memset (a,0,sizeof (a)), memset (b,0,sizeof (b)), BFS (1), BFS (Beg), en=beg;//find first endpoint for (i=1;i<=n i++) a[i]=dis[i];//record the distance from each point to this endpoint bfs (en);//Find another endpoint for (i=1;i<=n;i++) b[i]=dis[i];//records the distance of each point to this endpoint for (i=1;i<=n;i++) {ant=0;    ANT=MAXN (A[i],b[i]); printf ("%d\n", Ant);}} int main () {while (scanf ("%d", &n)!=eof) {getmap (); Solve ();} return 0;}

  

Hdoj 2196 Computer "the diameter of the tree asks for all the longest path starting from any node"

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.