The original stamp of this
Unlike previous and check sets, this time requires a delete operation. If the leaf node is OK, the direct modification of the Father pointer is good.
But if you move the root node, all the child nodes that point to it also change.
So add a virtual root node that will never be modified so that you can remove a point from the collection without affecting the other points.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7typedefLong LongLL;8 9 Const intMAXN =200000+Ten;Ten One intN, M; A - LL SUM[MAXN]; - intCNT[MAXN]; the intPA[MAXN]; - intFindset (intx) {returnx = = Pa[x]? X:PA[X] =Findset (pa[x]);} - - intMain () + { - while(SCANF ("%d%d", &n, &m) = =2&&N) + { A for(inti =1; I <= N; i++) {Pa[i] = i + N; sum[i + n] = i; cnt[i + n] =1; } at for(inti =1; I <= N; i++) Pa[i + N] = i +N; - - intop, x, y; - while(m--) - { -scanf"%d", &op); in if(OP = =1) - { toscanf"%d%d", &x, &y); + intpx = Findset (x), py =Findset (y); - if(px! =py) the { *PA[PX] =py; $Sum[py] + =sum[px];Panax NotoginsengCnt[py] + =cnt[px]; - } the } + Else if(OP = =2) A { thescanf"%d%d", &x, &y); + intpx = Findset (x), py =Findset (y); - if(px! =py) $ { $PA[X] =py; -Sum[py] + =x; -SUM[PX]-=x; thecnt[py]++; cnt[px]--; - }Wuyi } the Else - { Wuscanf"%d", &x); - intPX =Findset (x); Aboutprintf"%d%lld\n", cnt[px], sum[px]); $ } - } - } - A return 0; +}code June
UVa 11987 and check almost union-find