Tree DP Codevs 1814 longest chain

Source: Internet
Author: User

Codevs 1814 Longest chaintime limit: 1 sspace limit: 256000 KBtitle level: Diamonds DiamondTitle Description Description

Now give a tree n node two, ask the length of the longest chain in the binary tree, to ensure that the 1th node is the root of the two fork tree.

Enter a description Input Description

The 1th behavior of the input contains a positive integer n, which is the node number of the binary tree, and the node designator is 1 to N.

Next n rows, the line I row of the N row contains two positive integers l[i], r[i], representing the left son of the closing point I and the right son number. If L[i] is 0, the node I does not have a left son, similarly, if R[i] is 0 then it means there is no right son.

Output description Output Description

The output consists of 1 positive integers, the longest chain length for this binary tree.

Sample input Sample Input

5

2 3

4 5

0 6

0 0

0 0

Sample output Sample Output

4

Data range and Tips Data Size & Hint

"Sample description"

4-2-1-3-6 is one of the longest chains in this binary tree.

"Data Size"

For 10% of the data, there are n≤10;

For 40% of the data, there are n≤100;

For 50% of the data, there are n≤1000;

For 60% of the data, there are n≤10000;

For 100% of the data, there is n≤100000, and the depth of the tree is guaranteed not to exceed 32768.

Prompted

About binary tree:

Recursive definition of binary tree: Two The tree is either empty or composed of the root node, the left subtree, and the right sub-tree. Saozi Right subtree is a two-fork tree, respectively.

Note that there are three main differences between a root tree and a two-fork tree:

1. The number of nodes in a tree is at least 1, while the number of nodes in a binary tree can be 0;

2. There is no limit to the maximum number of nodes in the tree, and the maximum degree of the binary tree nodes is 2;

3. The nodes of the tree have no left and right points, while the nodes of the binary tree have left and right points.

About the longest chain:

The longest chain is one of the longest simple paths in this binary tree, that is, one path without repeating nodes. It is easy to prove that the starting and ending nodes of the longest chain in a binary tree are leaf nodes.

1 /*typical • Tree-shaped DP: the longest chain =max (all left child single chain Longest + right child single chain longest), the current node is the longest single chain is handed out, the current node of the single-chain longest =max (the child single chain the longest) +1;*/2 #defineN 1000103#include <iostream>4 using namespacestd;5#include <cstdio>6 intlian[n],ans=0;7 intL[n],r[n],n,c[n];8 voidinput ()9 {Tenscanf"%d",&n); One      for(intI=1; i<=n;++i) A     { -scanf"%d%d",&l[i],&r[i]); -     } the } - intDfsintk) - { -     if(k==0)return 0; +c[l[k]]=DFS (l[k]); -c[r[k]]=DFS (r[k]); +Ans=max (ans,c[l[k]]+c[r[k]]+1); A     returnC[k]=max (C[l[k]],c[r[k]) +1; at } - intMain () - { - input (); -Ans=max (DFS (1), ans); -printf"%d\n", ans-1); in     return 0; -}

Tree DP Codevs 1814 longest chain

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.