Uva11987-almost Union-find (and check whether the set is deleted)

Source: Internet
Author: User

Uva11987-almost Union-find (and check whether the set is deleted)

Question Link

Here are three operations: 1 p q indicates that the set of the two numbers p q is merged. 2 p q indicates that the number of P is taken out from the original set and put into the set where Q is located. 3 P indicates the total number of elements and the number of elements in the set where p is queried.

Solution: query the set. Only the operations that are not deleted in the check set. Therefore, we need to reduce the impact of the deleted vertex to 0, that is, apply for a new ID for the deleted vertex. In the future, this new ID will be used to represent this vertex, in this way, the point P in the original set is meaningless, and the natural impact is 0.

Code:

#include <cstdio>#include <cstring>const int maxn = 2e5 + 5;int n, m;int parent[maxn], cnt[maxn], sum[maxn];int id[maxn];int dex;void init () {    for (int i = 0; i <= n; i++) {        cnt[i] = 1;        sum[i] = id[i] = parent[i] = i;    }    dex = n;}int getParent (int a) {    return a == parent[a] ? a: parent[a] = getParent (parent[a]);}void Union (int a, int b) {    int p = getParent (a);    int q = getParent (b);    if (p != q) {        parent[p] = q;        cnt[q] += cnt[p];        sum[q] += sum[p];    }}void move (int a) {    int p = getParent (id[a]);    cnt[p]--;    sum[p] -= a;    id[a] = ++dex;    parent[dex] = dex;    cnt[dex] = 1;    sum[dex] = a;  }int main () {    int type;    int a, b;    while (scanf ("%d%d", &n, &m) != EOF) {        init();        while (m--) {            scanf ("%d", &type);            if (type == 1) {                scanf ("%d%d", &a, &b);                Union (id[a], id[b]);            } else if (type == 2) {                scanf ("%d%d", &a, &b);                int p = getParent (id[a]);                int q = getParent (id[b]);                if (p != q) {                    move(a);                    Union(id[a], id[b]);                }            } else {                scanf ("%d", &a);                int p = getParent (id[a]);                printf ("%d %d\n", cnt[p], sum[p]);            }        }    }    return 0;}

Uva11987-almost Union-find (and check whether the set is deleted)

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.