HDU 2196 Computer

Source: Internet
Author: User

Main topic

Give you a tree with a weighted edge, and find the distance from each point of the tree to its farthest point.

Analysis

Classic Tree DP problem. Because for a node, the path that might get the maximum distance from his subtree, or from his parent node, is used two times by DFS.

However, one problem is to judge the maximum value of a point from the parent node, then if the maximum value of his parent node is exactly from that point, then it loses the state from the parent node, so record the maximum of two values.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <string>5#include <iostream>6#include <algorithm>7#include <Set>8 #defineMAXN 200109 using namespacestd;Ten structNode One { A     intv,w; -     intNext; - }NODE[MAXN]; the intTot,n; - intHEAD[MAXN],MAXN[MAXN],SMAXN[MAXN],MAXID[MAXN],SMAXID[MAXN]; - voidAddintUintVintW) - { +node[tot].v=v; -node[tot].w=W; +node[tot].next=Head[u]; Ahead[u]=tot; attot++; - } - voidDFS1 (intUintp) - { -maxn[u]=0; -smaxn[u]=0; in      for(inti=head[u];i!=-1; i=node[i].next) -     { to         intv=node[i].v; +         if(v==p) -             Continue; the DFS1 (v,u); *         if(smaxn[u]<maxn[v]+NODE[I].W) $         {Panax Notoginsengsmaxn[u]=maxn[v]+NODE[I].W; -smaxid[u]=v; the             if(smaxn[u]>Maxn[u]) +             { A swap (smaxn[u],maxn[u]); the swap (smaxid[u],maxid[u]); +             } -         } $     } $ } - voidDFS2 (intUintp) - { the      for(inti=head[u];i!=-1; i=node[i].next) -     {Wuyi         intv=node[i].v; the         if(v==p) -             Continue; Wu         if(v==Maxid[u]) -         { About             if(node[i].w+smaxn[u]>Smaxn[v]) $             { -  -smaxn[v]=node[i].w+Smaxn[u]; -smaxid[v]=u; A                 if(smaxn[v]>Maxn[v]) +                 { the swap (smaxn[v],maxn[v]); - swap (smaxid[v],maxid[v]); $                 } the             } the         } the         Else the         { -             if(node[i].w+maxn[u]>Smaxn[v]) in             { thesmaxn[v]=node[i].w+Maxn[u]; thesmaxid[v]=u; About                 if(smaxn[v]>Maxn[v]) the                 { the swap (smaxn[v],maxn[v]); the swap (maxid[v],smaxid[v]); +                 } -             } the         }Bayi DFS2 (v,u); the  the     } - } - intMain () the { the      while(SCANF ("%d", &n)! =EOF) the     { thetot=0; -         intv,w; thememset (head,-1,sizeof(head)); the          for(intI=2; i<=n;i++) the         {94scanf"%d%d",&v,&W); the Add (i,v,w); the Add (v,i,w); the         }98DFS1 (1,-1); AboutDFS2 (1,-1); -          for(intI=1; i<=n;i++)101printf"%d\n", Maxn[i]);102 103     }104     return 0; the}

Diagram is saved with adjacency table, do not understand can see my previous blog

HDU 2196 Computer

Related Article

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.