Number of connected components (connected component _ and query set)

Source: Internet
Author: User
Number of connected components of zookeeper Crawling in process... Crawling failed Time limit:1000 ms Memory limit:65536kb 64bit Io format:% LLD & % llusubmitstatus

Description

In an undirected graph, if a path exists between the vertex VI and the vertex VJ, the VI is connected to the VJ. If any two vertices in the graph are connected, the graph is called a connected graph. Otherwise, the graph is called a non-connected graph. The connected subgraph is called a connected component, in this case, the number of vertices contained in a subgraph is very large. For example, an undirected graph has five vertices, 1-3-5 is connected, 2 is connected, and 4 is connected. Then the undirected graph has three connected components.

Input

The first line is an integer T, indicating that there are T group test samples (0 <t <= 50 ). The start line of each test sample contains two integers n, m, (0 <n <= 20, 0 <= m <= 200) representing n vertices and m edges respectively. The following m rows have two integers U, V, and vertex u connected to vertex v.

Output

Each line has an integer that represents the number of connected components.

Sample Input

23 11 23 23 21 2

Sample output

21

Hint

Check the set question and check the set at http://blog.csdn.net/u013486414/article/details/38682057;


#include <stdio.h>#include <string.h>#include <stdlib.h>int uset[10010];int rank[10010];int s[10010];void makeset(int n){    memset(uset,0,sizeof(uset));    for(int i=0;i<n;i++)        uset[i]=i;}int find(int x){    if(x!=uset[x])        uset[x]=find(uset[x]);    return uset[x];}void unionset(int x,int y){    if((x=find(x))==(y=find(y)))        return ;    if(rank[x]>rank[y])        uset[y]=x;    else    {        uset[x]=y;        if(rank[x]==rank[y])            rank[y]++;    }}int main(){    int T;    int n,m,i;    int a,b;    int f;    int sum=0;    scanf("%d",&T);    while(T--)    {        scanf("%d %d",&n,&m);        memset(s,0,sizeof(s));        makeset(n);        while(m--)        {            scanf("%d %d",&a,&b);            unionset(a,b);        }        for(i=0;i<n;i++)        {            f=find(i);            s[f]++;        }        sum=0;        for(i=0;i<n;i++)        {            if(s[i])                sum++;        }         printf("%d\n",sum);    }    return 0;}

Number of connected components (connected component _ and query set)

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.