and check the collection

Source: Internet
Author: User

Problem Description:

If n individuals participate in banquets (marking from 0 to N-1), 1 and 2 after Exchange, 1 on the understanding with 2, 2 and 3 after the Exchange, 2 and 3 to establish a cognitive relationship, due to the transfer of 1 and 3 also have a cognitive relationship. Because of the different interests between people, after a banquet, n individuals may form a number of small groups. The number of small groups formed after a banquet, given the number of people attending the banquet and the communication between the parties.

Limit:

Space complexity of O (n)

The input is read from the file named Sample.txt

For example, enter:

8

1 2

4 5

3 6

2 7

1 7

3 5

4 6

2 1

Output:

3


Because of the limited space complexity of O (n), we can only use one-dimensional arrays.

If the two elements are in the same group, the relationship of the tree is established for the two elements.

Imagine having a one-dimensional array, the length of which is the number of people attending the party, and each of the label 0~n-1 corresponds to the element subscript one by one in the array. Initially, the value of all elements in the array is set to-1, representing that each element is a root node, indicating that there are N groups at this time. If the label is a and the person with the label B Exchange, then find the label for a and the label for B of the group of the corresponding label, how to judge whether it is the root element. If the value of the element is-1, the root element is represented. So how do you find the root element of the group that a label a belongs to? First look at whether the value of the corresponding element of the label is-1, if it is, then it is the root element, then a is the label of the root element, if not-1, then its value is the label of the parent element, so we continue to find whether the parent element is the root element, and so on until the root element is found.

When we find the label corresponding to the root element of the group with the label A and the label B (assuming the m,n respectively), to determine whether the two labels are the same, if the same, continue to the next one, if not the same, that the two groups belong to the same group, you must merge the two groups, how to merge it. To make the root element of a group a child of the root element of another group, modify the value of the element labeled N to M so that the element labeled M becomes the parent of the element labeled N.

The following code is the label corresponding to the root element of the group to which the IDX belongs

 * * Find the root of each group-1 for root
/int find_root (int * arr, int idx)
{while
    (ARR[IDX)!=-1)
    {
        idx = arr[ IDX];
    }

    return idx;
}
The following is the body of the program:

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int count;
    FILE * infile = fopen ("Sample.txt", "R");
    FSCANF (infile, "%d", &count);
    int * arr = (int *) malloc (count * sizeof (int));
    for (int i = 0; i < count; ++i)
        arr[i] =-1;

    int a, B;
    while (FSCANF (infile, "%d%d", &a, &b)!= EOF)
    {
        int root_a = Find_root (arr, a);
        int root_b = Find_root (arr, b);
        if (root_a!= root_b)//description is not the same group
        {
            Arr[root_b] = root_a;//will be a combination of the root of Root_b and to the group with root_a as root
        }

    // At this point, the number of 1 in the array, that is, the number of groups
    int group_count = 0;
    for (int i = 0; i < count; ++i)
    {
        if (arr[i] = = 1)
        {
            group_count++
        }

    } printf ("%d", group_count);

    Free (arr);
    Fclose (infile);
    return 0;
}
The number of the last array-1 is the number of groups.

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.