题意:问给出的望塔之间的建造了围墙,将土地分成了几份

思路:用并查集判环,若有围墙相接的瞭望塔,有相同的父根,则存在环

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<iostream>
#include<string.h>
using namespace std;
int m,n,dp[1010];
int f(int xx)
{if(xx==dp[xx])return xx;elsereturn f(dp[xx]);
}
int dfs(int x,int y)
{int a=f(x);int b=f(y);if(a!=b){dp[b]=a;return 0;}elsereturn 1;
}
int main()
{while(cin>>m>>n){int sum=0;for(int i=0; i<m; i++)dp[i]=i;while(n--){int t1,t2;cin>>t1>>t2;sum+=dfs(t1,t2);}cout<<sum<<endl;}return 0;
}

Ice_cream's world I HDU - 2120(并查集判环)相关推荐

  1. LA3644简单并查集判环

    题意:       有n个化合物,每个化合物是两种元素组成,现在要装车,但是一旦车上的化合物中的某几个化合物组成这样一组关系,有n个化合物正好用了n中元素,那么就会爆炸,输入的顺序是装车的顺序,对于每 ...

  2. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  3. 【HDU - 1272】小希的迷宫 (并查集判环)

    题干: 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通 ...

  4. Codeforces 103B - Cthulhu(并查集 找环和块)

    传送门:https://codeforces.com/problemset/problem/103/B 题意: 给一个n个结点,m条边的无向图 判断给定图是否满足以下条件: 能被表示为有3个及以上的有 ...

  5. hdu 5606(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5606 解题思路: 这题实际上是要找一些连通的节点,使得边上的权值都为0. 很明显,首先把权值为0的边都 ...

  6. HDU 5441并查集 by cyl

    HDU 5441 题意: ​ 杰克喜欢旅游,从城市a到城市b是他最喜欢的,现在有n个城市m个路. ​ 当杰克在从城市a到城市b 的时候需要坐车,但是需要有等待时间t,他无法忍受超过x 的等待时间. ​ ...

  7. hdu 1232 并查集

    并查集是我暑假从高手那里学到的一招,觉得真是太精妙的设计了.以前我无法解决的一类问题竟然可以用如此简单高效的方法搞定.不分享出来真是对不起party了.(party:我靠,关我嘛事啊?我跟你很熟么?) ...

  8. hdu 4907(并查集)

    Task schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pr ...

  9. hdu 2473(并查集+删除操作)

    解题思路:这道题有并查集的删除操作,如果直接对这一棵树进行删除节点操作肯定是很困难的.所以可以建立虚拟节点,只要有一个节点要被删除,就直接把它投影到虚拟节点上,即用这个虚拟节点来代替我们要删除的节点. ...

最新文章

  1. im即时通讯源码_IM消息ID技术专题(六):深度解密滴滴的高性能ID生成器(Tinyid)
  2. 程序员javascript写乒乓球,和机器人练技术!没赢过!
  3. 《Android程序设计》一第一部分 工具和基础
  4. ad13批量安装元件库_ad16如何批量添加封装库?
  5. C语言实用算法系列之DOS传参“加减乘除计算器”
  6. atob和btoa的趣谈
  7. [Swift实际操作]八、实用进阶-(7)使用通知的方法进行对象间的消息传递
  8. python流程控制几种_python基础流程控制与数据类型
  9. 高等代数(邱维声):高等代数的研究对象
  10. 巧用 CSS 实现炫彩三角边框动画
  11. 论文阅读 -- unsupervised triplet hashing for fast image retrieval笔记
  12. 黑马程序员还收费 兄弟会都是免费的
  13. 如何编辑微信公众号(微信公众号内容制作流程)
  14. 运用python简单的画圆
  15. c/c++ read 函数和 write 函数
  16. 智能经济大局下,云智一体的“形胜”
  17. net core2.2 WebAP跨域解决方案
  18. MySql优化之路(一)--- 字段的设计和类型选择
  19. 电磁波谱和雷达应用波谱简述
  20. 将WORD中的文档编号转换成文字(宏命令法)

热门文章

  1. LeetCode之Max Consecutive Ones
  2. linux c之使用共享内存实现进程间通信
  3. js(javascript)取得当前时间小时,分钟,秒 以及毫秒
  4. php数据表格的重载,layui数据表格实现重载数据表格功能(搜索功能)
  5. 将太阳光聚集起来可以用来做饭!?
  6. 一个浪漫又悲情的爱情故事...
  7. 岛国小姐姐来例假时,男朋友背着她偷偷查手机......
  8. 不爱读书怎么办?用这个新奇的方法,熟知137亿年来的地球通史
  9. 《SAS编程与数据挖掘商业案例》学习笔记之六
  10. mysql utf8 bin设置_[mysql]修改collation为utf8_bin