Graph’s Cycle Component 并查集

Graph’s Cycle Component 

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

首先给你0---n-1个点,接下来告诉你那两个点之间是相连的。让你去判断它一共能产生几个连通分量,也就是子集,然后在判断哪些能成为circle graph。明显的可以看出若是circle

graph,成环的图,那么这个集合中所有的点的度都为二。那么就有了大致的解题思路了,首先通过并查集去判断有几个连通分量。然后再根据各分量中的点的度是否都为二

去判断是否为circle graph。

以前为了偷懒,懒得去写并查集中的find函数:找根的函数,结果使我在解题过程中遇到了自己无法解决的困难。

//错误代码

#include<cstdio>
#include<cstring> 
#include<algorithm>
using namespace std;
int m, n, ds[100005], edge[100005];
bool bo2[100005];
void init()
{
    for(int i=0;i<m;i++)
    {
        ds[i]=i;
        edge[i]=0;
        bo2[i]=0;
    }
}
int main()
{
    int a, b, i, j, c1, c2;
    while(scanf("%d%d", &m, &n), m||n)
    {
        init();
        for(i=0;i<n;i++)
        {
            scanf("%d%d", &a, &b);
            edge[a]++;
            edge[b]++;
            while(a!=ds[a])
            a=ds[a];
            while(b!=ds[b])
            b=ds[b];
            if(a!=b)
            {
                if(a>b)
                ds[a]=b;
                else
                ds[b]=a;
            }
        }
        c1=c2=0;
        for(i=0;i<m;i++)
        {
            if(ds[i]==i)
            {
                c1++;
                c2++;
            }
            // 错误代码
            if(bo2[ds[i]]==0&&edge[i]!=2)
            {
                c2--;
                bo2[ds[i]]=1;
            }
            // 错误代码
        }
        printf("%d %d\n", c1, c2);
    }
    return 0;
}

因此会使标记代码处出现可能会找不错根的问题。(如先给你2 3, 再给你1 2,那么ds[3]=2而不是1,导致出错

只需加上find函数,该函数还有一种递归写法

int find(int x)
{
    while(x!=ds[x])
        x=ds[x];
    return x;
}

int find(int x)
{
    whie(x!=ds[x])
        return find(ds[x]);
    return x;
}

然后改为

if(bo2[find(i)]==0&&edge[i]!=2)
{
    c2--;
    bo2[find(i)]=1;
}

即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值