<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:
- # Include <stdio. h>
- # Include <stdlib. h>
- # Define n 10001
- Int bleg [N]; // store the parent node of the current node
- Int Tran [N]; // number of times the storage is moved
- Int City [N]; // the city where the storage is located
- Int bnum [N]; // The total number of beads stored in the same location
- Int T;
- Int n, m;
- Void Init (); // Initialization
- Int find (int x); // query the set
- Void Union (int x, int y); // query the set and perform the T operation.
- Void Q (int x );
- Int main ()
- {
- Int Tn = 1;
- Int I, A, B;
- Char ch;
- Scanf ("% d", & T );
- While (t --)
- {
- Scanf ("% d", & N, & M );
- Init ();
- Printf ("case % d: \ n", TN ++ );
- For (I = 0; I <m; ++ I)
- {
- Scanf ("% C", & Ch );
- If (CH = 'T ')
- {
- Scanf ("% d", & A, & B );
- Union (A, B );
- }
- Else
- {
- Scanf ("% d", & );
- Q ();
- }
- }
- }
- Return 0;
- }
- Void Init () // Initialization
- {
- Int I;
- For (I = 1; I <= N; ++ I)
- {
- Bleg [I] = City [I] = I; // initialize the parent node and the city
- Tran [I] = 0;
- Bnum [I] = 1;
- }
- Return;
- }
- Int find (int x) // query the query set
- {
- Int y = X;
- Int Z;
- Int ntran = 0;
- While (y! = Bleg [y])
- {
- Ntran + = Tran [y]; // total number of times nodes are moved along the storage path
- Y = bleg [y];
- }
- While (X! = Bleg [x])
- {
- Z = bleg [x];
- Ntran-= Tran [X]; // prepare for the next node to update the number of moves
- Bleg [x] = y; // compress to the root node
- Tran [x] + = ntran; // synchronously update the number of moves
- X = z;
- }
- Return y;
- }
- Void Union (int x, int y) // query the set and perform the T operation.
- {
- Int FX = find (X );
- Int FY = find (y );
- Bleg [FX] = FY;
- + Tran [FX]; // The number of root nodes to be moved increases by 1
- Bnum [FY] + = bnum [FX]; // update the number of Dragon beads in the city
- Return;
- }
- Void Q (int x)
- {
- Int FX = find (X );
- Printf ("% d \ n", City [FX], bnum [FX], Tran [x]);
- Return;
- }