POJ- graph theory - disjoint-set template

POJ- graph theory - disjoint-set template

. 1, the init : the each element is initialized to a set of initialization of each parent node is its own element, each element of the ancestor node is itself (and may vary according to the situation).

void init()
{
    for ( int i = . 1 ; i <= n-; i ++) P [i] = i; // P [i] is the parent node of the node number i
}

2, the Find (the X-) : Find a set of elements is located, that is where the ancestors find a collection of this element to determine whether the two belong to the same set of elements, just look where they are the same set of ancestors can be. Merge two sets, but also a collection of the ancestor become the ancestors of another set.

int find(int x)
{
    return X == P [X] X:? P [X] = Find (P [X]); // Incl path compressor 
}

. 3, of Union (x, y)  : The combined x, y where the two sets, using find () to find the ancestor wherein two sets, one set of ancestors ancestor of another set point. Figure

void Union(int x, int y)
{
    x = find(x);
    y = find(y);

    IF (x == y) return ;
     the else P [y] = x; // the merged tree to the x y tree rooted at x 
}

POJ 2524 Ubiquitous Religions

Problem-solving ideas

Disjoint-set entry title, how many different connected components can be obtained there.

AC Code

#include<cstdio>
const int N = 50500;

int P [N]; // parent group of nodes 
int n-, m; // number of nodes and the node relationship

void init()
{
    for ( int I = 0 ; I <n-; I ++) P [I] = I; // initializes the parent node itself, which is an isolated single junction tree 
}

int the Find ( int x) // find the root of the tree where x 
{
     return (x == the p-[x]) x:? the p-[x] = the Find (the p-[x]);
}


void of Union ( int x, int y) // combined, y x tree to tree merge 
{
    x = find(x);
    y = find(y);
    if (x == y)return;
    else p[y] = x;
}

int main ()
{
    int cnt = 1;
    while (scanf("%d%d", &n, &m)!=EOF)
    {
        if (n == 0 && m == 0) break;
        int x, y;
        the init (); // initialize, clear the array 
        for ( int I = 0 ; I <m; I ++ )
        {
            scanf("%d%d", &x, &y);
            Union (x, y);
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            if (i == p[i])ans++;
        }
        printf("Case %d: %d\n", cnt++, ans);
    }
    return 0;
}

POJ 1611 The Suspects

Problem-solving ideas

As a team more than one person, the second person from the merger where the individual is like the first group.

AC Code

#include<cstdio>
const int N = 30300;

int P [N]; // parent group of nodes 
int n-, m; // number of nodes and the node relationship

void init()
{
    for ( int I = 0 ; I <n-; I ++) P [I] = I; // initializes the parent node itself, which is an isolated single junction tree 
}

int the Find ( int x) // find the root of the tree where x 
{
     return (x == the p-[x]) x:? the p-[x] = the Find (the p-[x]);
}


void of Union ( int x, int y) // combined, y x tree to tree merge 
{
    x = find(x);
    y = find(y);
    if (x == y)return;
    else p[y] = x;
}

int main ()
{
    while (scanf("%d%d", &n, &m)!=EOF)
    {
        if (n == 0 && m == 0) break;
        int groupNum, x, y;
        the init (); // initialize, clear the array 
        the while (M-- )
        {
            scanf("%d%d", &groupNum, &x);
            while (--groupNum)
            {
                scanf("%d", &y);
                Union (X, Y); // for each crew members where a first group merge 
            }
        }
        int ANS = 0 ;
         for ( int I = 0 ; I <n-; I ++) // myself patients 
        {
             IF (Find (I) Find == ( 0 )) ANS ++; // and related student No. 0 
        }
        printf("%d\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/yun-an/p/11101947.html