HDU 3635 fig Algorithm
Sun Wukong is looking for the Dragon Pearl. This is the enhanced version of the Dragon Pearl, because these dragon beads will be derived, and more than seven dragon beads will last.
Wukong started with the Dragon Pearl radar detector of buma, but found that the program of the Dragon Pearl radar detector of buma was too junk. So we found these ACM experts to write a program for the Dragon Pearl radar detector, it is required that the location of the city where a Dragon Ball is located be displayed. The number of dragons and beads in the city where the Dragon Ball is located have been moved.
Buma is a rich man. I am asking for 5 million yuan to write a program. Because this program needs to use the Union Find (and query set) algorithm, and the most difficult part is how to compress the path, it is naturally easy to do without compressing the path, to compress the path can make the program much faster, therefore, the price is 5 million, and O (cost _ money) O Haha ~.
The idea of path compression is:
Add an additional number of records to update the child node when searching for the parent node.
After the compression path is compressed, the program block is over 100 ms. The effect is still acceptable.
Without compressing the path, you can directly connect to a tree. When calculating the number of moves, you can directly calculate the distance to the parent node.
The flexible use of Union Find makes it difficult to reach 5 stars if it is well optimized.
#include
const int MAX_N = 10001;struct Subset{int xp, yNumBall, zTrans, addTrans;};Subset subs[MAX_N];int N;void initSubs(){for (int i = 1; i <= N; i++){subs[i].xp = 0;subs[i].yNumBall = 1;subs[i].zTrans = 0;subs[i].addTrans = 0;//the main help for path compression}}int findParent(int x)//with path compression{if (subs[x].xp)//do not use 0 index.{int p = subs[x].xp;subs[x].xp = findParent(p);//update parentsubs[x].zTrans += subs[p].addTrans;//careful:Update transportation timessubs[x].addTrans += subs[p].addTrans;//Watch out: +=, not =return subs[x].xp;}return x;}int findParent_2(int x)//without path compression{int p = x;if (subs[x].xp)//do not use 0 index.{p = findParent(subs[x].xp);//update parentsubs[x].zTrans = subs[subs[x].xp].zTrans+1;//Update transportation times}return p;}void unionTwo(int x, int y){int xp = findParent(x);int yp = findParent(y);if (xp == yp) return ;subs[xp].xp = yp;//It should be yp, not y. add to its parent directlysubs[yp].yNumBall += subs[xp].yNumBall;//only record balls in parentsubs[xp].addTrans++;//transportation happen, add one more transsubs[xp].zTrans++;//Watch out: update self's trans}int main(){int T, Q, a, b;//N cities and balls, Q questschar cmd;scanf("%d", &T);for (int t = 1; t <= T; t++){scanf("%d %d", &N, &Q);initSubs();printf("Case %d:\n", t);while (Q--){getchar();// get rid of '\n'scanf("%c %d", &cmd, &a);if (cmd == 'T'){scanf("%d", &b);unionTwo(a, b);}else{int ap = findParent(a);printf("%d %d %d\n", ap, subs[ap].yNumBall, subs[a].zTrans);}//balls recorded in parent, trans times recorded in self node}}return 0;}