HDU 3635 dragon bils

Source: Internet
Author: User

Question address:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3635


Question type: and query set

Question:

Five hundred years later, the number of Dragon bils will increase unexpectedly, so it's too difficult for Monkey King (Wukong) to gather all of the dragon bils together.



His country has n cities and there are exactly n dragon bils in the world. at first, for the ith Dragon Ball, the sacred dragon will puts it in the ith city. through long years, some
Cities 'Dragon ball (s) wocould be transported to other cities. To save physical strength Wukong plans to take flying nimbus cloud, a magical flying cloud to gather dragon bils.
Every time Wukong will collect the information of one Dragon Ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon
Bils are there in that city, you also need to tell him how many times the ball has been transported so far.


Sample input:

23 3T 1 2T 3 2Q 23 4T 1 2Q 1T 1 3Q 1

Sample output:

Case 1:2 3 0Case 2:2 2 13 3 2



Question:

The Dragon Ball with N numbers is 1 ~ N. At the beginning, the numbers are 1 ~ N city.

Then, if you enter 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.

Enter q a, then the number of dragons in the city where A is located, the number of dragons in the city where A is located, and the number of times a is moved.


Ideas and summary:

First of all, I feel that this question is indeed a good question and can be used to query and compress the path.

This question is a bit tangled. After wa many times, it means that I still have insufficient understanding about the query set.

Find (x) the root node of X, that is, the city where X is located.

It is easy to find the number of dragons in a city. You only need to add a rank array to indicate and query the rank of the Set and the height of the tree, that is, the number of dragons in that city.

However, it is difficult to find the number of longzhu mobile, and it is also my tangle.


First, when a city's dragon beads are all moved to another city, the number of Dragon beads in this city will change to 0, that is to say, no dragon beads are in this city, and no longer will be moved to the city.

This empty city. Because t a X must be moved to the city where Long Zhu X is located, and now no long Zhu has fallen into the empty city, no long Zhu X will fall into the empty city. Finally, we can draw a conclusion:

The city can only be moved once.

Open an array num to indicate the number of moves for each request.

If t a B is set for every move, the initial ball in the city where B is located (that is, the root node of B) is the first move of the ball, set the number of moves of the initial ball to 1.

This is not enough. This only increases the number of initial balls at the root node, and the number of other balls at the root node has not been added.


Don't worry. This step can be left for path compression.

When path compression is not performed, its father is the same with the node. At this time, the debt that has been owed has also been paid back, increase the number of movements of all its ancestors (this is the key and a comparison

Difficult to understand ).

Finally, it only needs new nodes. The number of times is also new.

After the path is compressed, the parent node of each ball is the city where it is located.


# Include <iostream> # include <cstdio> # include <cstring> # define maxn 10002 using namespace STD; int N, Q, Father [maxn], rank [maxn], num [maxn]; void Init () {for (INT I = 1; I <= N; ++ I) {FATHER [I] = I, rank [I] = 1, num [I] = 0 ;}} int find (INT X) {If (x = Father [x]) return X; int T = Father [X]; father [x] = find (father [x]); num [x] + = num [T]; return father [X];} void Union (int x, int y) {int A = find (x); int B = find (y); if (! = B) {FATHER [a] = B; rank [B] + = rank [a]; num [a] = 1; // This is the first time ball a moves} int main () {# ifdef local freopen ("input.txt", "r", stdin); # endif int T, A, B, k, CAS = 1; char cmd [2]; scanf ("% d", & T); While (t --) {printf ("case % d: \ n ", CAS ++); scanf ("% d", & N, & Q); Init (); For (INT I = 0; I <q; ++ I) {scanf ("% s", CMD); If (CMD [0] = 'T') {scanf ("% d", & A, & B ); union (a, B) ;}else {scanf ("% d", & K); int x = find (k); int CNT = 0; printf ("% d \ n", X, rank [X], num [k]) ;}} return 0 ;}



-- The meaning of life is to give it meaning.
Original Http://blog.csdn.net/shuangde800 ,
D_double





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.