Title Address:
http://acm.hdu.edu.cn/showproblem.php?pid=3635
Topic Type: And check the collection
Topic:
Five hundred years later, the number of Dragon Balls would increase unexpectedly, so it's too difficult for monkey King (wuk ONG) to gather all of the dragon balls together.
His country has n cities and there are exactly n dragon Balls into the world. At a, for the ith Dragon Ball, the sacred Dragon'll puts it in the ith city. Through long years, some cities ' Dragon Ball (s) would is transported to the other cities. To save physical strength Wukong plans to take flying Nimbus, a Cloud magical flying to Cloud gather.
Every Time Wukong'll collect the information of one Dragon Ball, he'll ask for you this information of that ball. You are must tell him which city the ball are located and how many dragon balls are in this city, where there also to tell H Im how to many the ball has been transported so far.
Sample input:
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
Sample output:
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
The main effect of the topic:
There are N dragon beads, numbered 1~n, which were initially distributed in the city numbered 1~n.
Then, if you enter t a B, then move, and move all the Dragonball in the city where the number a dragon bead is located to the city where the number B Dragon bead is located.
Input q A, then output a dragon bead in the city, a dragon bead in the city of the number of Dragon beads, and a dragon beads move the number of times.
Ideas and Summary:
First of all, feel this problem is really a good and check the set of topics, you can deepen the collection and compression path understanding.
To do this problem a little bit tangled, WA a lot of times, indicating that I do not understand the collection and the understanding is not enough.
Ask for a dragon bead in that city to do well, only need find (x), found the root node of x, that is the city where x
The number of dragon beads in a city is also very good to do, just add a rank array, to indicate and check the rank of the set, and the height of the tree, that is the city's Dragon beads.
And ask a dragon bead move the number of times, more difficult to make, also formally I tangled place.
First of all, when the dragon bead of a city is moved to another city, the number of the Dragon beads in the city becomes 0, that is, no dragonball is in the city, then there will be no more dragon beads to move to this empty city. Because t a x, must be moved to the city of Dragonball X, and now there is no dragon bead fell in the empty city, for any dragon Bead X, will not fall in the empty city. Finally, it can be concluded that a city can only move once.
Open an array of num to indicate the number of moves for each request.
Every time you move, it's t a B, and the most original ball of B's city (i.e., B's root node) is the first move of the ball, and the number of moves to the initial ball is set to 1.
It's not enough to do that alone. Because it only increases the number of times the initial ball is in the root node and the number of other balls in the same node has not been added.
Don't worry, this step can be left to the path compression when done.
Before the path compression, its father was the same as the previous node, and at this time, the previous debt has also been owed, let it move the number of times plus it all the ancestors of the movement (this is the key, but also more difficult to understand).
Finally, it only thought of new nodes. The number of times is also new.
After the path is compressed, the Father node of each ball is the city in which 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 (a!=b) {Father[a] = b;
RANK[B] + = Rank[a]; Num[a] = 1;
This is the first time the ball A is moved} 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%d", &n,&q);
Init ();
for (int i=0; i<q; ++i) {scanf ("%s", cmd); IF (cmd[0]== ' T ') {scanf ("%d%d", &a, &b);
Union (A, b);
} else{scanf ("%d", &k);
int x = find (k);
int cnt=0;
printf ("%d%d%d\n", X, Rank[x],num[k]);
}} return 0; }