hdoj 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): 741    Accepted Submission(s):
429

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

#include<stdio.h>
#include<string.h>
#define MAX 1010
int set[MAX];
int sum=0;
int find(int fa)
{
	int t;
	int ch=fa;
	while(fa!=set[fa])
	fa=set[fa];
	while(ch!=fa)
	{
		t=set[ch];
		set[ch]=fa;
		ch=t;
	}
	return fa;
}
void mix(int x,int y)
{
	int fx,fy;
	fx=find(x);
	fy=find(y);
	if(fx!=fy)
	set[fx]=fy;
	else
	sum++;
}
int main()
{
	int n,m,i,a,b;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=0;i<=n;i++)
		set[i]=i;
		for(i=0;i<m;i++)
	    {
		    scanf("%d%d",&a,&b);
		    mix(a,b);
	    }
	    printf("%d\n",sum);
	    sum=0;
	}
	return 0;
}

  

hdoj 2120 Ice_cream's world I

时间: 2024-08-05 07:08:32

hdoj 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

hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】

称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向图.然后找一个点.到全部点的距离和最小.找出这个点并输入距离. 分析:非常明显是求一个最小树形图,可是没有说根节点.要找跟节点,我们能够虚拟一个节 点 x .x 到全部节点连边距离为前面全部距离和+1为 dis . 然后从x 节点求一次最小树形图为ans,则ans - dis 就是最小树形图的距离. 假设图不

hdu 2120 Ice_cream&#39;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,

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>

hdoj 2121 Ice_cream’s world II 【无根节点最小树形图】

题目:hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向图,然后找一个点,到所有点的距离和最小,找出这个点并输入距离. 分析:很明显是求一个最小树形图,但是没有说根节点,要找跟节点,我们可以虚拟一个节 点 x ,x 到所有节点连边距离为前面所有距离和+1为 dis . 然后从x 节点求一次最小树形图为ans,则ans - dis 就是最小树形图的距离. 如果图不连通,或者ans>=2*dis 说明不存在,都则与 x 点出发的边就是结果点 A

hdoj 2122 Ice_cream’s world III 【最小生成树】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1254    Accepted Submission(s): 414 Problem Description ice_cream's world becomes stronger and stronger; every road is buil

HDOJ 2121 Ice_cream’s world II 最小树形图无根树

朱刘算法 最小树形图无根树: 建立一个虚拟的根节点,向所有节点连边,权值为其他所有边的权值和+1 在求最小树形图的时候,记录和虚拟的根相连的是哪个节点 在这题中,边是从小往大加的所以直接记录的是相连的是第几号边.... Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3442    Accept

hdoj 2122 Ice_cream’s world III【最小生成树】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1237    Accepted Submission(s): 408 Problem Description ice_cream's world becomes stronger and stronger; every road is buil