UVa 11987 almost Union-find (with support for delete operations and check sets)

Source: Internet
Author: User

Portal

Description

I hope you know the beautiful union-find structure. In this problem, you ' re-implement something similar, but not identical. The data structure need to write is also a collection of disjoint sets, supporting 3 operations:

    • 1 P Q Union The sets containing p and Q. If p and Q is already in the same set, ignore this command.
    • 2 p Q Move p to the set containing Q. If p and Q is already in the same set, ignore this command.
    • 3 p Return The number of elements and the sum of elements in the set containing P.

Initially, the collection contains n sets: {1}, {2}, {3}, ..., {n}.

Input

There is several test cases. Each test case begins with a line containing the integers n and M (1≤n, m≤100, and), the number of integers, and the n Umber of commands. Each of the next m lines contains a command. For every operation, 1≤p, Q≤n. The input is terminated by End-of-file (EOF).

Output

For each type-3 command, Output 2 integers:the number of elements and the sum of elements.

Explanation

Initially: {1}, {2}, {3}, {4}, {5}

Collection after Operation 1 1 2: {$}, {3}, {4}, {5}

Collection after Operation 2 3 4: {--}, {3,4}, {5} (we omit the empty set that was produced when taking out 3 from {3})

Collection after Operation 1 3 5: {}, {3,4,5}

Collection after Operation 2 4 1: {1,2,4}, {3,5}

Sample Input







3 3
Sample Output


2 8
Ideas

Test instructions

Give the number of 1-n, start each number from the beginning to form a set, ask to support the operation

    • Merges the set of elements p with the collection of element q
    • Move element p to the collection where element q is located
    • asks how many elements the set of elements P is in, and how many are

Exercises

The first and third operations are very bare and check the set, the key is to operate 2, it is easy to think that the element P moved to the set of elements Q, you can first in the element p is set to delete it, and then the element p and the element Q in the collection merge. The key to this step is to delete the operation, if p is a leaf node, the direct change of P's father point on the line, but P is where the root node of the collection and when it is deleted, its child nodes need to re-establish the relationship, the process is still a certain complexity. We can take another thought: two-time hashing (ReHash), for each node has a hash value, before the search needs to convert the X to its hash value hash[x], then when the deletion, as long as the X's hash value is changed, Become a value that never appears (you can do this with a counter), and then set up the new value, because it is only one element, so it must be a new collection. This ensures that the time complexity of each delete operation is O (1), and does not break the original tree structure, the only drawback is that every deletion of a node is actually more than one piece of memory, if the deletion operation is unlimited, then memory will grow indefinitely.

Simply put, it is the creation of a virtual node, so that the original node is still in the tree, and will not destroy its structure, open a id[] array to represent the value of element x.

#include <bits/stdc++.h>using namespace Std;const int maxn = 100005;int CNT,FA[MAXN],NUM[MAXN],SUM[MAXN],ID[MAXN ];void init (int N) {for (int i = 0;i <= n;i++) {fa[i] = id[i] = Sum[i] = I;num[i] = 1;} CNT = N;} int find (int x) {int r = x;while (r! = Fa[r]) R = Fa[r];int i = x,j;while (i! = r) {j = fa[i];fa[i] = R;i = j;} return r;} void Union (int x,int y) {int fx = find (Id[x]), FY = Find (Id[y]), fa[fx] = fy;num[fy] + = Num[fx];sum[fy] + = Sum[fx];}  void Delete (int x) {int fx = find (Id[x]),--num[fx];sum[fx] = x;id[x] = ++cnt,fa[id[x]] = id[x],num[id[x] [= 1,sum[id[x]] = x;} int main () {int n,m;while (~scanf ("%d%d", &n,&m)) {int Opt,x,y;init (N), while (m--) {scanf ("%d", &opt); if (opt = = 1) {scanf ("%d%d", &x,&y), if (Find (id[x])! = Find (Id[y])) Union (x, y);} else if (opt = = 2) {scanf ("%d%d", &x,&y), if (Find (id[x])! = Find (Id[y])) Delete (x), Union (y);} ELSE{SCANF ("%d", &x);p rintf ("%d%d\n", Num[find (Id[x])],sum[find (Id[x])]) return 0;}

  

UVa 11987 almost Union-find (with support for delete operations and check sets)

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.