zoukankan      html  css  js  c++  java
  • Hdu 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): 1275    Accepted Submission(s): 499


    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
     
    //并查集判断连通分量数,连通分块的每个度数为2代表为圈图
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cmath>
    #define N 100003
    using namespace std;
    int f[N];
    int r[N];
    int find_r(int x)
    {
       if(f[x]>=0)
       {
         return  f[x]=find_r(f[x]);
       }
       return x;
    }
    void union_set(int x,int y)
    {
        x=find_r(x);
    	y=find_r(y);
    	if(x==y) return ;
    	if(r[x]>r[y])
    	   f[y]=x;
    	 else 
    		 if(r[x]==r[y])  
    		 {
    	         f[y]=x;
    			 r[x]++;
    		 }
    		 else
    			 f[x]=y;
    
    }
    int de[N];
    int main()
    {
    	int i,n,m;
    	int x,y;
    	int r_n,r_m;
       while(scanf("%d%d",&n,&m),n||m)
       {
    	   for(i=0;i<n;i++)  //开始这用memset()函数显示超时、、看来、、、
    	   {
    	      de[i]=r[i]=0;
    		  f[i]=-1;
    	   }
           for(i=0;i<m;i++)
    	   {
    		   scanf("%d%d",&x,&y);
    	        union_set(x,y);
    			de[x]++;de[y]++;
    	   }
    	   
    	   for(i=0;i<n;i++)
    		   if(de[i]!=2)
    		   {
    		       r_m=find_r(i);
    			   r[r_m]=-1;
    		   }
    		   r_n=r_m=0;
             for(i=0;i<n;i++)
    		   if(f[i]==-1)
    		   {
    		       r_n++;
    			   if(r[i]!=-1)
    				   r_m++;
    		   }
    		printf("%d %d\n",r_n,r_m);
       }
       return 0;
    }
  • 相关阅读:
    Redis安装过程
    TDDL-剖析淘宝TDDL
    jQuery 遍历
    util-C# 复杂条件查询(sql 复杂条件查询)查询解决方案
    util-判断当前年份所处的季度,并返回当前季度开始的月份
    省市区(县)三级联动代码(js 数据源)
    JS:实用功能
    C#常用格式输出
    算术运算符
    C# 中的结构类型(struct type)
  • 原文地址:https://www.cnblogs.com/372465774y/p/2578592.html
Copyright © 2011-2022 走看看