题目:
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
题意:
ice_cream的世界是一个富裕的国家,它拥有许多肥沃的土地。 今天,冰淇淋女王想要将土地授予勤奋的ACMers。 所以有一些瞭望塔被建立起来,并且建造了瞭望塔之间的墙壁,以便划分冰淇淋的世界,没有墙壁交叉。 一个围墙包围的土地必须只给一个ACMer,求出最多可以分到土地的ACMer的数量。
思路:
用并查集,然后判断环,一个环就是一块地;
代码如下:
#include<stdio.h>
#include<string.h>
int n,m,a,b;
int f[1001];
void init()//初始化,自己的祖宗是自己;
{
for(int i=0; i<n; i++)
f[i]=i;
return ;
}
int getf(int v)//查询祖宗;
{
if(f[v]==v)
return v;
else
return f[v]=getf(f[v]);
}
int merge(int v,int u)//判断
{
int t1=getf(v);
int t2=getf(u);
if(t1==t2)//同一个祖宗,有一个环,就有一块地;
return 1;
f[t2]=t1;
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
int i,sum=0;
for(i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
sum+=merge(a,b);//统计;
}
printf("%d\n",sum);
}
return 0;
}