zoukankan      html  css  js  c++  java
  • HDU 3560 Graph’s Cycle Component

    Graph’s Cycle Component

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1655    Accepted Submission(s): 637

    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
     
    Author
    momodi@WHU
     
    Source
     
    Recommend
    zhouzeyong   |   We have carefully selected several similar problems for you:  3559 3558 3563 3562 3561 
     
     
    思路:并查集+环路检查,我竟然不知道what is a cycle graph?看了别人的说法才知道是一个什么东西,然后我觉得应该先求出连通分量,然后求cycle graph,
    但是竟然不行,只能使用并查集,但是并查集怎么可能保证这样呢,Two vertices belong to a same component if and only if those two vertices connect
    each other directly or indirectly,并查集确定的component,不能保证connect each other 吧。
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    int map[110000];
    int indegree[110000];
    int the_last_conponent;
    int the_last_cycle;
    int n,m;
    int a,b;
    int find(int x)
    {
        while(x != map[x])
        {
            x = map[x];
        }
        return x;
    }
    void Merge(int a,int b)
    {
        int p = find(a);
        int q = find(b);
        if(p < q)
            map[q] = p;
        else
            map[p] = q;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m) != EOF && (n + m))
        {
            for(int i = 1;i <= n;i ++)
               map[i] = i;
            memset(indegree,0,sizeof(indegree));
            for(int i = 1;i <= m;i ++)
            {
                scanf("%d%d",&a,&b);
                a ++;b ++;
                indegree[a] ++;
                indegree[b] ++;
                Merge(a,b);
            }
            the_last_conponent = 0;
            the_last_cycle = 0;
            for(int i = 1;i <= n;i ++)
                if(map[i] == i)
                    the_last_conponent ++;
            for(int i = 1; i <= n;i ++)
            {
                if(indegree[i] != 2)
                    map[find(i)] = 0;
            }
            for(int i = 1;i <= n;i ++)
                if(map[i] == i)
                    the_last_cycle ++;
            printf("%d %d
    ",the_last_conponent,the_last_cycle);
        }
        return 0;
    }
  • 相关阅读:
    剖析 GSM 加密机制以及位置更新的过程
    利用ASK/OOK 发射模块,实现信号重放
    使用RTL-SDR打开车门
    复现 360 Unicorn Team 黑科技之 HackNFC
    如何搭建并使用便携式 4G/LTE 伪基站研究移动安全
    如何利用 LTE/4G 伪基站+GSM 中间人攻击攻破所有短信验证
    黑客炼金术士 Seeker:可以攻破 4G 摸到你短信,还要为朝阳群众提供谍战工具
    如何使用HackRF做一个简单的IMSI捕获器
    招中高级web开发工程师
    ionic 动画和返回按钮
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3425983.html
Copyright © 2011-2022 走看看