Computer HDU, computerhdu

Source: Internet
Author: User

Computer HDU, computerhdu

Computer HDU-2196

Question: The longest chain with any vertex in the tree as the endpoint. Note that the read is not an edge table, but the length of the father and the father edge of each node (except No. 1.

Method:

The longest chain starting from each endpoint. The first is to go down (toward the child node), and the second is to go to the parent node. Process them separately. (Depending on the Program)

(It seems that the practice is not beautiful at all... What is the longest chain ...)

Error (local): 23 rows are missing and the original longest chain is not updated to the next long chain.

1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 using namespace std; 5 struct Edge 6 {7 int to, next, w; 8} e [20100]; 9 int f1 [10100], ne, fa [10100], a1 [10100], a2 [10100], b1 [10100], ww [10100], b2 [10100], ansu [10100]; 10 // a1 [I], b1 [I] respectively record the length of the longest chain down to node I, node I sub-node ID of I that goes down to the longest chain 11 // a2 [I], b2 [I] records the length of the next long chain of node I (ensure that no public node exists with the longest chain except node I ), node I subnode ID 12 of the subnode passing through the next long chain // ww [I] records the edge value 13 of node I to the parent node // ansu [I] records the node I goes up to the longest chain 14 in T n; 15 void dfs (int x, int fa) 16 {17 for (int k = f1 [x]; k! = 0; k = e [k]. next) 18 if (e [k].! = Fa) 19 {20 dfs (e [k]. to, x); // first obtain a1, b1, a2, and b2 from the subnode, and then use the subnode to update its own 21 if (a1 [e [k]. to] + e [k]. w> a1 [x]) // if the chain length is 22 {23 a2 [x] = a1 [x], b2 [x] = b1 [x], // The new secondary chain is known as the maximum chain 24 a1 [x] = a1 [e [k]. to] + e [k]. w, b1 [x] = e [k]. to; // The New longest chain is the chain 25} 26 else if (a1 [e [k]. to] + e [k]. w> a2 [x]) // if the chain is no longer than the longest known chain, but longer than the known chain, 27 a2 [x] = a1 [e [k]. to] + e [k]. w, b2 [x] = e [k]. to; // The new secondary long chain is the chain formed by pointing itself to the edge of the subnode and adding the sub-node down to the longest chain 28} 29} 30 void dfs2 (int x, int fa) // The current node number and the current node father number 31 {32 if (fa! = 0) // if it is the root node, there is no upstream link; otherwise there will be 33 {34 int t1; 35 if (x = b1 [fa]) 36 t1 = a2 [fa]; 37 else38 t1 = a1 [fa]; // t1 indicates the longest chain length down the Father's Day point. If the longest chain below Father's Day goes through x, then t1 becomes the next long chain length 39 on Father's Day. // The longest chain length 40 ansu [x] = max (t1, ansu [fa]) + ww [x]; 41 // The longest chain to which the current node is directed, that is, the chain formed by the current node pointing to the father's side and the longest chain to which the Father's Day points up, A chain formed by adding t1 to the edge of the current node pointing to the father. The longer one is 42} 43 for (int k = f1 [x]; k! = 0; k = e [k]. next) 44 if (e [k].! = Fa) 45 dfs2 (e [k]. to, x); // this time, you must first find the parent node to find the child node, which is different from the first dfs () 46} 47 int main () 48 {49 int I, x, y; 50 while (scanf ("% d", & n) = 1) 51 {52 ne = 0; 53 memset (f1, 0, sizeof (f1); 54 memset (fa, 0, sizeof (fa); 55 memset (a1, 0, sizeof (a1); 56 memset (a2, 0, sizeof (a2); 57 memset (ansu, 0, sizeof (ansu); 58 for (I = 2; I <= n; I ++) 59 {60 scanf ("% d", & x, & y); 61 e [++ ne]. to = x; 62 e [ne]. next = f1 [I]; 63 e [ne]. w = y; 64 f1 [I] = ne; 65 e [++ ne]. to = I; 66 e [ne]. next = f1 [x]; 67 e [ne]. w = y; 68 f1 [x] = ne; 69 ww [I] = y; 70} 71 dfs (1, 0 ); // obtain the longest chain and secondary chain 72 dfs2 () for all nodes to go down; // obtain the longest chain 73 for (I = 1; I <= n; I ++) 74 printf ("% d \ n", max (a1 [I], ansu [I]); // The last answer is a long 75 in the longest chain that goes down and up at a certain point} 76 77 return 0; 78}

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.