Algorithm template: Union collection

Union Collection

Title description
There are a total of n numbers, numbered from 1 to n, and each number is in a set at the beginning.
Now there are m operations to be performed, and there are two operations:
"M ab", merge the sets where the two numbers numbered a and b are located, if the two numbers are already in the same set, ignore this operation;
"Q ab", asking whether the two numbers numbered a and b are in the same set;

#include<iostream>

using namespace std;
const int N = 1e6 + 10;
int p[N];

```cpp
int find(int x)
{
    
    
    if(p[x] != x)p[x] = find(p[x]);
    return p[x];
}

int main(void)
{
int n,m;
cin>>n>>m;
for(int i = 1 ; i <= n ; i ++ )p[i] = i;

while(m--)
{
    char op[2];
    int a, b;
    scanf("%s%d%d" ,op,&a,&b);
    if(op[0] == 'M')
    {
        p[find(a)] =find(b);
    }
    else if(op[0] == 'Q')
    {
        if(find(a) == find(b))puts("Yes");
        else puts("No");
    }
}

}

**并合集**
一种快速对字符串以及二进制数组元素的管理方式,用树的形式进行存储;
**代码分析**
对于1~n这些数字,我们创建了一个数组p来记录他的祖先节点,运用下标与实际数字进行关联,例如p[i]记录的就是i的祖先节点,在程序一开始的时候,我们将每个节点的祖先设置为他自己,然后通过合并数组的操作实现树的建立;
*集合的合并*
树的根节点存储集合的信息,p数组的单元存储节点的祖先,将根节点的祖先修改为另一个集合就完成了就和的合并;

```cpp
int find(int x)
{
    if(p[x] != x)p[x] = find(p[x]);
    return p[x];
}

This operation completes the search for the ancestor root node of x

Guess you like

Origin blog.csdn.net/ZMTH010123/article/details/114439563