Graph’s Cycle Component (并查集)

来源:互联网 发布:帝国cms生成html 编辑:程序博客网 时间:2024/06/04 00:43

Problem Description
In graph theory, a cycle graph is an undirected graph that consists of a single cycle, or in other words, some number of vertices connected in a closed chain.
Now, you are given a graph where some vertices are connected to be components, can you figure out how many components are there in the graph and how many of those components are cycle graphs.
Two vertices belong to a same component if and only if those two vertices connect each other directly or indirectly.

Input
The input consists of multiply test cases. The first line of each test case contains two integer, n (0 < n < 100000), m (0 <= m <= 300000), which are the number of vertices and the number of edges. The next m lines, each line consists of two integers, u, v, which means there is an edge between u and v. You can assume that there is no multiply edges and no loops. The last test case is followed by two zeros, which means the end of input.

Output
For each test case, output the number of all the components and the number of components which are cycle graphs.

Sample Input
8 9
0 1
1 3
2 3
0 2
4 5
5 7
6 7
4 6
4 7
2 1
0 1
0 0

Sample Output
2 1
1 0
Sample Output
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
题意:找出一个无向图中有几个连通块和几个环,环讲的是一个联通块中每个点的度都是2,一个独立的点也是联通快;
思路;用数组记录每个点的度,点数,边数,联通块可用简单并查集判断,判断环只需先判断连通块中点是否与边数相等,再判断每个点的度是否为2就行了;
代码:

#include<cstdio>#include<algorithm>using namespace std;int a[100005],b[100005];//b是度数;struct node{   int vag,flag,t;  //vag是点数,flag判断是否度数超过2的,t是边数;}str[100005];int find(int t){    if(t!=a[t])       a[t]=find(a[t]);    return a[t];}void mark(int x,int y){    b[x]++;b[y]++;    int zx=find(x);    int zy=find(y);    if(zx==zy)    {        if(b[x]>2||b[y]>2)           str[zx].flag=1;        str[zx].t++;        return;    }    if(str[zx].vag<str[zy].vag)    {        a[zx]=zy;        str[zy].vag+=str[zx].vag;        str[zx].vag=0;        str[zy].t++;        str[zy].t+=str[zx].t;        str[zx].t=0;        if(str[zx].flag)        {            str[zy].flag=1;            str[zx].flag=0;        }        if(b[x]>2||b[y]>2)           str[zy].flag=1;    }    else    {        a[zy]=zx;        str[zx].t++;        str[zx].vag+=str[zy].vag;        str[zy].vag=0;        str[zx].t+=str[zy].t;        str[zy].t=0;        if(str[zy].flag)        {            str[zx].flag=1;            str[zy].flag=0;        }        if(b[x]>2||b[y]>2)           str[zx].flag=1;    }}int main(){    int n,m;    while(scanf("%d%d",&n,&m),n||m)    {        for(int i=0;i<n;i++)        {            a[i]=i;            b[i]=0;            str[i].flag=0;            str[i].vag=1;            str[i].t=0;        }        for(int i=0;i<m;i++)        {            int za,zb;            scanf("%d%d",&za,&zb);            mark(za,zb);        }        int s1=0,s2=0;        for(int i=0;i<n;i++)        {            if(str[i].vag)              s1++;            if(str[i].vag==str[i].t&&str[i].vag!=0)            {                if(!str[i].flag)                  s2++;            }        }        printf("%d %d\n",s1,s2);    }}
0 0
原创粉丝点击