Graph’s Cycle Component (并查集)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值