# Query the pku2492a Bug's Life Issue Report.

Source: Internet
Author: User
 This question is almost the same as 1703, that is, whether the vertex between two points is an even number or a base number, the key is to record the weights of each vertex (that is, the number of layers of each vertex in the tree )...For example, if two strings are 1-2-3 and 4-5-6-7, it is a headache to change the weights of points in the merged string, as long as this problem is solved, the problem will be solved.A typical idea is to use Fa [I] to represent the parent node of I. path compression is performed here, so each parent node of I is the root node of the tree where it is located .. PV [I] is used to represent the weight of I, that is, the number of layers in the tree where node I is located (in fact, due to path compression, all trees have only two layers, and one layer is the root node, A layer is a subnode, but for calculation, the layer where each node should be located is noted down )...The core code is:Void Init (int n ){Int I;For (I = 0; I <= N; I ++ ){Fa [I] =-1; // initialize that the parent node of each node is-1, and that of each node is 0;PV [I] = 0;}}Int find (int I ){Int R;If (Fa [I] <0)Return I; // If I is the root node, the value of I is returned.R = find (Fa [I]); // find the root node of IPV [I] + = PV [Fa [I]; // note that this step is not very important. If it is a plus of a point and a point, this step does not work, because Fa [I] must be the root node, and its weight is 0, but if the child nodes of the two trees are connected, such as 1-2-3, 4-5-6-7. then the connection between 3 and 6 is very important, because after the connection, the power of 4 is changed to 2, and the book of 567 also needs to change. This change is not a direct change, instead, each subsequent find operation adds 2 to 567 to form the connected layers !!!Fa [I] = R;Return R;}Void unit (int A, int B ){Int Ra = find (a); // adjust the weights of A and B to the obtained weights through the find operation.Int RB = find (B );If (Pv [a]> PV [B]) {// if 1-2 is connected to 2-3, the weights of 2 and 3 must be determined, otherwise, enter 2, 3, and 2 to get different answers...Fa [RB] = Ra;PV [RB] = PV [a]-PV [B] + 1;}Else {Fa [Ra] = RB;PV [Ra] = PV [B]-PV [a] + 1;}} AC code:# Include # Define M1 2005Int Fa [M1], PV [M1];Void Init (int n ){Int I;For (I = 0; I <= N; I ++ ){Fa [I] =-1;PV [I] = 0;}}Int find (int I ){Int R;If (Fa [I] <0)Return I;R = find (Fa [I]);PV [I] + = PV [Fa [I];Fa [I] = R;Return R;}Void unit (int A, int B ){Int Ra = find ();Int RB = find (B );If (Pv [a]> PV [B]) {Fa [RB] = Ra;PV [RB] = PV [a]-PV [B] + 1;}Else {Fa [Ra] = RB;PV [Ra] = PV [B]-PV [a] + 1;}}Void main (){Int I, N1, A, B, Count = 0, flag, M, N;Scanf ("% d", & N1 );While (N1 --){Flag = 1;Count ++;Scanf ("% d", & N, & M );Init (N );For (I = 0; I # Deprecision Max 30001Int Fa [Max], down [Max], up [Max];Void Init (){Int I;For (I = 0; I
Related Keywords:

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.