HDU 2196 computer (tree DP)

Source: Internet
Author: User

Given a tree, find the maximum path length that each node can reach.

Analysis: Taking the numbered I node as an example (non-root node), the longest path length is only two possibilities, 1) the longest path exists in the subtree; 2) the longest path exists in the path of the parent node

Therefore, you only need to find the two paths corresponding to each node to obtain the maximum value. Of course, the root node only has the first possibility.

View code

# Include <iostream>
# Include <algorithm>
# Include < String >
# Include <vector>
Using Namespace STD;
Const Int N = 10010 ;
Int N, DIST [N], DP [N];
Struct Edge
{
Int V, W, h;
Edge (){}
Edge ( Int A, Int B, Int C = 0 ): V (a), w (B), H (c ){}
};
Struct Node
{
Int W, ID;
Node (){}
Node ( Int A, Int B): w (A), ID (B ){}
};
Vector <edge> G [N];
Void Init ()
{
For ( Int I = 0 ; I <= N; I ++)
G [I]. Clear ();
}
Void Dfs1 ( Int U)
{
Int Size = G [u]. Size ();
Dist [u] = 0 ;
Node mdis1 = node ( 0 , 0 ), Mdis2 = node ( 0 , 0 );
// Mdis1 and mdis2 respectively record the longest path length in the U subtree and the branch number
For ( Int I = 0 ; I <size; I ++)
{
Int V = G [u] [I]. V;
Dfs1 (v );
Node T = node (G [u] [I]. W + dist [v], I );
If (T. W> mdis1.w)
{
Mdis2 = mdis1;
Mdis1 = T;
}
Else If (T. W> mdis2.w)
Mdis2 = T;
}
Dist [u] = mdis1.w;
For ( Int I = 0 ; I <size; I ++)
{
If (I = mdis1.id)
G [u] [I]. H = mdis2.w; // Record the longest path except the branch where the son of I is located in the subtree.
Else G [u] [I]. H = mdis1.w;
}
}
Void Dfs2 ( Int U)
{
Int Size = G [u]. Size ();
For ( Int I = 0 ; I <size; I ++)
{
Int V = G [u] [I]. V;
If (U = 1 )
{
DP [v] = G [u] [I]. H + G [u] [I]. W;
}
Else {
DP [v] = max (DP [u], G [u] [I]. h) + G [u] [I]. W; // The maximum length of the path stored by Father's Day
}
Dfs2 (v );
}
}
Int Main ()
{
Int X, W;
While (Scanf ( " % D " , & N) = 1 )
{
Init ();
For ( Int I = 2 ; I <= N; I ++)
{
Scanf ( " % D " , & X, & W );
G [X]. push_back (edge (I, W, 0 ));
}
Dfs1 ( 1 );
Memset (DP, 0 , Sizeof (DP ));
DP [ 1 ] = DIST [1 ];
Dfs2 ( 1 );
For ( Int I = 1 ; I <= N; I ++)
Printf ( " % D \ n " , Max (DP [I], DIST [I]);
}
Return 0 ;
}

 

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.