HDU 2120 Ice_cream's world I(并查集,环的个数,冰激凌王国有什么?)

来源:互联网 发布:淘宝试用中心在哪儿 编辑:程序博客网 时间:2024/05/05 20:09

Ice_cream's world I(题目)

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1254    Accepted Submission(s): 750


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 100 11 21 32 43 40 55 66 73 64 7
 

Sample Output
3
 

Author
Wiskey
 

Source
HDU 2007-10 Programming Contest_WarmUp
 



题意:

这翻译也是够了,大意就是女王分土地的问题吧。(有道翻译的没明白)直接过思路吧。


思路:

想到判断有几个环在图中,这个题目就迎刃而解了。


代码:

#include<stdio.h>#define MYDD 10000int pre[MYDD];void init(int x) {for(int j=0; j<x; j++)pre[j]=j;}int find(int x) {//根节点查找函数 return x==pre[x]? x:find(pre[x]);}bool combine(int x,int y) {//判断是否成环int fx=find(x);int fy=find(y);if(fx==fy)//根节点相同,成环return true;else {pre[fx]=fy;return false;}}int main() {int n,m;while(scanf("%d%d",&n,&m)!=EOF) {init(n);//初始化 int ans=0;while(m--) {int a,b;scanf("%d%d",&a,&b);if(combine(a,b))//成环 ans++;}printf("%d\n",ans);}return 0;}


后:

别人一分钟学会的,你花十分钟掌握也行,不怕走得慢,就怕怠慢。

***********************************************************************************


0 0
原创粉丝点击