解题思路:给出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    Accepted Submission(s): 371

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

  1. #include<stdio.h>
  2. #include<string.h>
  3. int pre[10005],a[10005];
  4. int find( int root)
  5. {
  6. if(root!=pre[root])
  7. pre[root]=find(pre[root]);
  8. return pre[root];
  9. }
  10. void unionroot( int root1,int root2)
  11. {
  12. int x,y;
  13. x=find(root1);
  14. y=find(root2);
  15. if(x!=y)
  16. pre[x]=y;
  17. }
  18.  
  19. int main()
  20. {
  21. int root1,root2,x,y,i,n,m,tmp;
  22. while(scanf("%d %d",&n,&m)!=EOF)
  23. {
  24. tmp=0;
  25. for(i=0;i<=10005;i++)
  26. pre[i]=i;
  27. while(m--)
  28. {
  29. scanf("%d %d",&root1,&root2);
  30. x=find(root1);
  31. y=find(root2);
  32. if(x==y)
  33. tmp++;
  34. unionroot(x,y);
  35. }
  36.  
  37. printf("%d\n",tmp);
  38.  
  39. }
  40. }

  

HDU 2120 Ice_cream's world I【并查集】的更多相关文章

  1. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  2. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  3. hdu 2120 Ice_cream's world I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2120 Ice_cream's world I Description ice_cream's worl ...

  4. HDU 2120 Ice_cream&#39;s world I(并检查集合)

    职务地址:HDU 2120 这题尽管字数不多,但就是看不懂. . 意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就能够了. 代码例如以下: #include <i ...

  5. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  6. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  7. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...

  8. hdu 4619 Warm up 2(并查集)

    借用题解上的话,就是乱搞题.. 题意理解错了,其实是坐标系画错了,人家个坐标系,我给当矩阵画,真好反了.对于题目描述和数据不符的问题,果断相信数据了(这是有前车之鉴的hdu 4612 Warm up, ...

  9. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  10. hdu 5652 India and China Origins 并查集+逆序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...

随机推荐

  1. 弹出ifame页面(jquery.reveal.js)

    <body> <a data-reveal-id="myModalDailyModify" data-animation="fade" tit ...

  2. for循环的写法及优化

    最近这几天在研究浏览器性能的时候发现了一些小知识,在此做一总结: 其中经常用到的for循环有:正常的for循环,for in循环,for of循环等,但是对于正常的for循环可以做一下优化,使得其在执 ...

  3. wechat4j开发所有jar包

    wechat4j开发所需要的jar包合计,不用你去单独下载,已经全部包括 下载连接 wechat4j-lib.rar 如果你的服务是部署在新浪云计算SAE上的,那么下载这个jar合集 wechat4j ...

  4. 常用几个空格的 Unicode 码

    const SPACE_UNICODE = { 'ensp': '\u2002', 'emsp': '\u2003', 'nbsp': '\u00a0' }

  5. Aspose.Cells基础使用方法整理

    Aspose.Cells 插件,将web端数据以excel形式导出到客户端. 相关文档: https://blog.csdn.net/djk8888/article/details/53065416 ...

  6. struts 中数据处理的3中方式

    方式一: 获取servletapi中的对象 方式二: struts中封装的对象 方式三: 实现接口 方式一和方式二的区别 方式一需要额外引入包或者是方式二实现不了的功能,比如:获取url 因为方式二只 ...

  7. C#追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件

    C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw ...

  8. post数据html数据获取危险处理办法

      基础小知识 ValidateRequest属性是Page类中比较常用的属性,用来指示是否对输入数据进行潜在危险性检查.在默认情况下为True,就是表示 “是对输入的数据进行潜在危险性检查”,这个属 ...

  9. [NOIPlus]斗地主

    毫无意义的一道题. 用pai[i]表示某种点数的牌的剩余量,used[i]表示单,对,三,四的出牌数,大力分类讨论,大力dfs即可...真奇葩... #include <iostream> ...

  10. 自动合法打印VitalSource Bookshelf中的电子书

    最近有一本2千多页的在VitalSource中的电子书想转为PDF随时阅读,没料网上找了一圈没有找到合适的.相对好一些的只有一个用Python写的模拟手动打印.于是想到了用AutoHotkey写一个自 ...