HDOJ 题目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): 2112    Accepted Submission(s): 775

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

[email protected]

Source

2010 ACM-ICPC Multi-University Training Contest(12)——Host
by WHU

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  3559 3558 3563 3562 3561

ac代码

#include<stdio.h>
#include<string.h>
int pre[100010],dig[100010];
int n,m;
/*int find(int x)
{
	int r=x;
	while(r!=pre[r])
		r=pre[r];
	int j,k;
	j=x;
	while(j!=r)
	{
		k=pre[j];
		pre[j]=r;
		j=k;
	}
	return r;
}*/
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF,n||m)
	{
		//init();
		//memset(dig,0,sizeof(dig));
		int i,j;
		for(i=1;i<=n;i++)
		{
			pre[i]=i;
			dig[i]=0;
		}
		for(i=0;i<m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			u++;v++;
			dig[u]++;
			dig[v]++;
			//memge(u,v);
			int fa=u;
			while(fa!=pre[fa])
				fa=pre[fa];
			int fb=v;
			while(fb!=pre[fb])
				fb=pre[fb];
			if(fa!=fb)
			{
				if(fa<fb)
				{
					pre[fb]=fa;
				}
				else
					pre[fa]=fb;
			}
		}
		int ans1=0,ans2=0;
		for(i=1;i<=n;i++)
		{
			if(pre[i]==i)
			{
				ans1++;
			}
		}
		for(i=1;i<=n;i++)
		{
			if(dig[i]!=2)
			{
				int fi=i;
				while(fi!=pre[fi])
				{
					fi=pre[fi];
				}
				pre[fi]=0;//设为-1会超时,,,,,
			}
		}
		for(i=1;i<=n;i++)
		{
			if(pre[i]==i)
				ans2++;
		}
		printf("%d %d\n",ans1,ans2);
	}
}
时间: 2024-08-27 10:50:42

HDOJ 题目Graph’s Cycle Component(并查集)的相关文章

Num 21 : HDOJ: 题目1272 : 小希的迷宫 ( 并查集问题 )

题目: 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35368    Accepted Submission(s): 10808 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通

【HDOJ】3560 Graph’s Cycle Component

并查集的路径压缩. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 100005 5 6 int deg[MAXNUM], bin[MAXNUM]; 7 char isCycle[MAXNUM]; 8 9 int find(int x) { 10 int r = x; 11 int i = x, j; 12 13 while (r != bin[r]) 14 r = bin[r]; 15 16 while

hdoj 1116 Play on Words 【并查集】+【欧拉路】

Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5622    Accepted Submission(s): 1850 Problem Description Some of the secret doors contain a very interesting word puzzle. The team

Bipartite Graph hdu 5313 bitset 并查集 二分图

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset     [用法详情:http://blog.csdn.net/piaocoder/article/details/47177891] 用时:624ms 思路: 二分图的总边数即:n*m(假设一个有n个点,另一个有m个点) 题目是给出总共的点数为n,间接求最大的边数 想到一个小学题:给出长度为n的绳子,

Hdoj 1213 How Many Tables 【并查集】

How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16590 Accepted Submission(s): 8117 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner tim

CodeForces 466E Information Graph --树形转线性+并查集

题意:有三种操作: 1.新增一条边从y连向x,此前x没有父节点 2.x接到一份文件,(文件标号逐次递增),然后将这份文件一路上溯,让所有上溯的节点都接到这份文件 3.查询某个节点x是否接到过文件F 解法: 首先要知道一个性质,节点u在v的上溯路径上的话要满足: L[u]<=L[v] && R[u] >= R[v] (先进后出) 先将所有的边都读入,dfs得出L[u],R[u],然后将查询分为tot类(tot=总文件种数),记录每一类有那些地方查询了,然后如果type=2,那么记

hdu 1598 find the most comfortable road(并查集+枚举)

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4899    Accepted Submission(s): 2131 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超

hdu 1272 小希的迷宫(简单并查集)

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31396    Accepted Submission(s): 9726 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是

【Openjudge】岛屿(并查集)

题目链接 此题是并查集.考虑到水位不断上涨,所以将时间倒转.先统计最后一天的联通块个数,每一天浮出水面的块进行计算.复杂度O(玄学). 代码如下 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; short u[5]={0,0,1,0,-1}; short w[5]={0,1,0,-1,0}; int n,m; inline long long read(){ long