Strategic game POJ - 1463 dfs

 

That Italy + Solution:

  1  // . 5
   2  // 1 1
   . 3  // 2 1
   . 4  // . 3 1
   . 5  // 1 1
   . 6  // assign five points from the second row to the fifth row below (referred to as i-th row), each row two numbers x, y. It indicates there is an edge between i and x. This is the length of one side of the y-
   7  // you need to find out the maximum value of each point apart with all the points. And in the final output.
  8  // should note that this is a tree, why do I say. Because you only n-1 edges, and each point has at least one edge
   9  
10  // so we can find out with bfs from a random point x distance from other points. Where x and then find the point of maximum distance x1, and then he went to run again bfs
 11  // find out from this point x1 away from the biggest point x2. Pitted again bfs. Let them take the maximum output from it
 12  // 
13  // because the first time you find that point is certainly the deepest leaf node x1 map. Looking for another leaf node x2 use this x1, other points of maximum distance from all points of the affirmative
 14 // between these two points. . Since these two points is the maximum depth of 
15 #include <stdio.h>
 16 #include < String .h>
 . 17 #include <the iostream>
 18 is #include <algorithm>
 . 19 #include <Queue>
 20 is  the using  namespace STD;
 21 is  const  int MAXN = 20010. ;
 22 is  int CNT, head [MAXN], deap1 [MAXN], VIS [MAXN], deap2 [MAXN], n-, deap3 [MAXN];
 23 is Queue < int > R & lt;
 24  struct Edge
 25  {
 26 is      int U, V, Next, W;
 27 }e[maxn];
 28 void init(int deap[maxn])
 29 {
 30     for(int i=1;i<=n;++i)
 31         deap[i]=0;
 32 }
 33 void add_edge(int x,int y,int z)
 34 {
 35     e[cnt].u=x;
 36     e[cnt].v=y;
 37     e[cnt].w=z;
 38     e[cnt].next=head[x];
 39     head[x]=cnt++;
 40 }
 41 void bfs(int x,int deap[maxn])
 42 {
 43     while(!r.empty()) r.pop();
 44     init(vis);
 45     vis[x]=1;
 46     r.push(x);
 47     while(!r.empty())
 48     {
 49         int u=r.front();
 50         r.pop();
 51         for(int i=head[u];i!=-1;i=e[i].next)
 52         {
 53             int v=e[i].v;
 54             if(!vis[v])
 55             {
 56                 vis[v]=1;
 57                 deap[v]=deap[u]+e[i].w;
 58                 r.push(v);
 59             }
 60         }
 61     }
 62 }
 63 int main()
 64 {
 65     while(~scanf("%d",&n))
 66     {
 67         memset(head,-1,sizeof(head));
 68         cnt=0;
 69         for(int i=2;i<=n;++i)
 70         {
 71             int x,y;
 72             scanf("%d%d",&x,&y);
 73             add_edge(i,x,y);
 74             add_edge(x,i,y);
 75         }
 76         init(deap1);
 77         bfs(1,deap1);
 78         int ans1=0,id1=-1;//printf("**\n");
 79         for(int i=1;i<=n;++i)
 80         {
 81             if(ans1<deap1[i])
 82             {
 83                 ans1=deap1[i];
 84                 id1=i;
 85             }
 86         }
 87         init(deap2);
 88         if(id1!=-1)
 89         {
 90             bfs(id1,deap2);
 91         }
 92         ans1=0,id1=-1;
 93         for(int i=1;i<=n;++i)
 94         {
 95             if(ans1<deap2[i])
 96             {
 97                 ans1=deap2[i];
 98                 id1=i;
 99             }
100         }
101         init(deap3);
102         if(id1!=-1)
103         {
104             bfs(id1,deap3);
105         }
106         for(int i=1;i<=n;++i)
107         {
108             printf("%d\n",max(deap1[i],max(deap2[i],deap3[i])));
109         }
110     }
111     return 0;
112 }

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11666552.html