HDOJ-3635-Dragon ballproblem solving report

Source: Internet
Author: User

<Knowledge Sharing>

This is a question of investigating and querying the path compression of a set. In Wukong's world, there are n dragons and N cities (numbered from 1 to n). At first, X-Dragon placed each Dragon Ball in the corresponding city. Wukong wants to collect dragon beads, but these dragon beads are sometimes transferred. You need to tell Wukong some information about longzhu. Now we have another T group test. Each test group has a n (number of dragons and cities) and a Q (number of operation actions). There are two types of operation actions:

T a B. Move all the dragon beads in the city where the Dragon beads numbered a are located to the city where the Dragon beads numbered B are located. The two cities are different.

Q a, Wukong needs to know X (the number of the city where longzhu A is located), y (the number of longzhu in the city numbered X), and Z (the number of longzhu transfers numbered)

 

Solution: first, we need to know three pieces of information based on the number of each Dragon Ball, the number of the city where the Dragon Ball is located, and the number of times the Dragon Ball has been transferred. First, let's analyze the T operation. First, the two cities must be different. Then, when we move all the dragons to another city, there must be a Dragon Ball that was already in that city and has never been moved at one time. This Dragon Ball is the root node. Similarly, the root node Dragon Ball has never been moved and will not move all the dragon beads in the city to an empty city. With this rule, we can maintain the number of Dragon beads in the city where the root node is located to find the X and Y of each Dragon Ball in the city. As for Z, We need to compress the corresponding node to the root node during path compression, and the corresponding Z should also be updated synchronously, each time we move, we only move the number of root nodes of the city to be moved more than 1. Then, when the path is compressed, the sum of the number of moving nodes along the way (including the nodes to be compressed) this is the actual number of times the node is moved.

 

The solution code is as follows:


  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Define n 10001
  4. Int bleg [N]; // store the parent node of the current node
  5. Int Tran [N]; // number of times the storage is moved
  6. Int City [N]; // the city where the storage is located
  7. Int bnum [N]; // The total number of beads stored in the same location
  8. Int T;
  9. Int n, m;
  10. Void Init (); // Initialization
  11. Int find (int x); // query the set
  12. Void Union (int x, int y); // query the set and perform the T operation.
  13. Void Q (int x );
  14. Int main ()
  15. {
  16. Int Tn = 1;
  17. Int I, A, B;
  18. Char ch;
  19. Scanf ("% d", & T );
  20. While (t --)
  21. {
  22. Scanf ("% d", & N, & M );
  23. Init ();
  24. Printf ("case % d: \ n", TN ++ );
  25. For (I = 0; I <m; ++ I)
  26. {
  27. Scanf ("% C", & Ch );
  28. If (CH = 'T ')
  29. {
  30. Scanf ("% d", & A, & B );
  31. Union (A, B );
  32. }
  33. Else
  34. {
  35. Scanf ("% d", & );
  36. Q ();
  37. }
  38. }
  39. }
  40. Return 0;
  41. }
  42. Void Init () // Initialization
  43. {
  44. Int I;
  45. For (I = 1; I <= N; ++ I)
  46. {
  47. Bleg [I] = City [I] = I; // initialize the parent node and the city
  48. Tran [I] = 0;
  49. Bnum [I] = 1;
  50. }
  51. Return;
  52. }
  53. Int find (int x) // query the query set
  54. {
  55. Int y = X;
  56. Int Z;
  57. Int ntran = 0;
  58. While (y! = Bleg [y])
  59. {
  60. Ntran + = Tran [y]; // total number of times nodes are moved along the storage path
  61. Y = bleg [y];
  62. }
  63. While (X! = Bleg [x])
  64. {
  65. Z = bleg [x];
  66. Ntran-= Tran [X]; // prepare for the next node to update the number of moves
  67. Bleg [x] = y; // compress to the root node
  68. Tran [x] + = ntran; // synchronously update the number of moves
  69. X = z;
  70. }
  71. Return y;
  72. }
  73. Void Union (int x, int y) // query the set and perform the T operation.
  74. {
  75. Int FX = find (X );
  76. Int FY = find (y );
  77. Bleg [FX] = FY;
  78. + Tran [FX]; // The number of root nodes to be moved increases by 1
  79. Bnum [FY] + = bnum [FX]; // update the number of Dragon beads in the city
  80. Return;
  81. }
  82. Void Q (int x)
  83. {
  84. Int FX = find (X );
  85. Printf ("% d \ n", City [FX], bnum [FX], Tran [x]);
  86. Return;
  87. }

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.