hdu 2120 Ice_cream's world I(判断是否有环,简单的并查集)

Ice_cream‘s world I

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 603    Accepted Submission(s): 347

Problem Description

ice_cream‘s world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are set up, and wall between watchtowers be build, in order to partition
the ice_cream’s world. But how many ACMers at most can be awarded by the queen is a big problem. One wall-surrounded land must be given to only one ACMer and no walls are crossed, if you can help the queen solve this problem, you will be get a land.

Input

In the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between
A and B has a wall(A and B are distinct). Terminate by end of file.

Output

Output the maximum number of ACMers who will be awarded.

One answer one line.

Sample Input

8 10
0 1
1 2
1 3
2 4
3 4
0 5
5 6
6 7
3 6
4 7

Sample Output

3

Author

Wiskey

Source

HDU 2007-10 Programming Contest_WarmUp

题意:

找到围成的环的个数。

代码如下:

#include<stdio.h>
int father[10010];
int find(int r)
{
	if(r!=father[r])
	return find(father[r]);
	else
	return father[r];
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		int a,b,count=0;
		while(m--)
		{
			scanf("%d%d",&a,&b);
			int fa=find(a);
			int fb=find(b);
			if(fa==fb)
			count++;//判断是否有环,有的话,两者的父节点应该相等
			else
			father[fa]=fb;
		}
		printf("%d\n",count);
	}
	return 0;
}

hdu 2120 Ice_cream's world I(判断是否有环,简单的并查集)

时间: 2024-10-24 12:22:37

hdu 2120 Ice_cream's world I(判断是否有环,简单的并查集)的相关文章

hdu 2120 Ice_cream&#39;s world I

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2120 Ice_cream's world I Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are s

HDU 2120 Ice_cream&#39;s world I【并查集】

解题思路:给出n对点的关系,求构成多少个环,如果对于点x和点y,它们本身就有一堵墙,即为它们本身就相连,如果find(x)=find(y),说明它们的根节点相同,它们之间肯定有直接或间接的相连,即形成环 样例的示意图 共3个环 Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 642    Acce

HDU 2120 Ice_cream&#39;s world I(并查集)

题目地址:HDU 2120 这题虽然字数不多,但就是看不懂..意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就可以了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h>

hdu 3342 Legal or Not (判断是否存在环)

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4229    Accepted Submission(s): 1875 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

hdu 1863(畅通工程)(简单的并查集,模板)

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17933    Accepted Submission(s): 7589 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

hdu 1213 (How Many Tables)(简单的并查集,纯模板)

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

HDU OJ 5326 Work( 2015多校联合训练第3场) 并查集

题目连接:戳ME #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int M = 1e2+5; int n, k; int par[M]; int sum[M]; void find(int x) { if( par[x]!=x ) { sum[par[x]]++; find(par[x]); } else return; } int main() {

HDU 3371 Connect the Cities(并查集+Kruskal)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先联通了几个点,所以为了判断连通性 很容易想到用并查集+kruskal. 不过要注意 这题有一个坑点,就是边数很多 上限是25000,排序的话可能就超时了.而点数则比较少 上限是500,所以很多人选择用Prim做.但我个人觉得这样连通性不好判断.其实边数多没关系,我们主要去重就好啦,用邻接矩阵存下两点

hdu 1856 并查集(路径压缩)

hdu 1856 More is better 简单的并查集,用到了路径压缩 题意:找出节点数最多的连通分支,并输出该节点数. 思路:统计每个连通分支的节点数(利用并查集构建图时进行路径压缩,用一个与根节点下标对应的sum数组记录节点数),比较后得出最大值. 1 #include<cstdio> 2 #include<cstring> 3 4 int set[10000000 + 50]; 5 int sum[10000000 + 50]; 6 bool visit[1000000