UVa 11987 and check almost Union-find

Source: Internet
Author: User

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

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.