点击打开链接

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.

InputIn 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. OutputOutput 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
AC代码
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4
 5 using namespace std;
 6
 7 const int maxn=1010;
 8
 9 int n,m;
10 int father[maxn];
11
12 void makeSet(){
13     for(int i=0;i<n;i++){
14         father[i]=i;
15     }
16 }
17
18 int findSet(int x){
19     if(x!=father[x]){
20         father[x]=findSet(father[x]);
21     }
22     return father[x];
23 }
24
25 int main(){
26
27     //freopen("input.txt","r",stdin);
28
29     while(~scanf("%d%d",&n,&m)){
30         makeSet();
31         int ans=0;
32         int a,b;
33         while(m--){
34             scanf("%d%d",&a,&b);
35             int fx=findSet(a);
36             int fy=findSet(b);
37             if(fx==fy)
38                 ans++;
39             else
40                 father[fx]=fy;
41         }
42         printf("%d\n",ans);
43     }
44     return 0;
45 }

做这道题快有一年了,可是现在看来还是那么的陌生,真不知道自己这一年到底学到了什么?自己是不是在虚度光阴?

这有一个并查集详解,讲得很好

点击打开链接

转载于:https://www.cnblogs.com/ruruozhenhao/p/7406460.html

G - Ice_cream's world I (并查集)相关推荐

  1. ccpc长春-K. Ragdoll(启发式合并、并查集、gcd转化)map的神奇tle

    K. Ragdoll 菜死我得了 题意:给定一组点以及他们的权重(1⩽n⩽1051\leqslant n\leqslant10^51⩽n⩽105),起初所有点都是独立的点.给定三种操作: 1xv1\ ...

  2. 暑期集训5:并查集 线段树 练习题G: HDU - 1754

    2018学校暑期集训第五天--并查集 线段树 练习题G  --   HDU - 1754 I Hate It 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让 ...

  3. Ice_cream's world I HDU - 2120(并查集判环)

    题意:问给出的望塔之间的建造了围墙,将土地分成了几份 思路:用并查集判环,若有围墙相接的瞭望塔,有相同的父根,则存在环 ice_cream's world is a rich country, it ...

  4. G. Columns Swaps(并查集)

    G. Columns Swaps 题目大意: 两行nnn列,一次操作可以交换同列两个数,要求最小操作次数使两行都是1−n1-n1−n的排列. 前置知识: 2-sat 思路: 每个数都要出现两次这是必要 ...

  5. Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)

    题目链接  2016 ACM-ICPC EC-Final Problem G 题意  给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的 ...

  6. P2906 [USACO08OPEN]Cow Neighborhoods G 切比雪夫距离 + 并查集 + set

    传送门 考虑将曼哈顿距离转换成切比雪夫距离,这样问题就变成了max(∣x1−x2∣,∣y1−y2∣)≤dmax(|x_1-x_2|,|y_1-y_2|)\le dmax(∣x1​−x2​∣,∣y1​− ...

  7. Portals G【并查集】

    >Link luogu P7528 >Description >解题思路 求使每一条边相连通的最小哞尼值 认真想一想,就会发现初始情况,本来就相连通的边会形成一个个环(一个点只有两条 ...

  8. 并查集 问题 G: 极品飞车XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Fl

    转载于https://www.cnblogs.com/liyinggang/p/5474341.html 负责解说 题目描述 XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Ai ...

  9. 【BZOJ1015】【JSOI2008】星球大战 并查集

    题目大意 给你一张\(n\)个点\(m\)条边的无向图,有\(q\)次操作,每次删掉一个点以及和这个点相邻的边,求最开始和每次删完点后的连通块个数. \(q\leq n\leq 400000,m\le ...

最新文章

  1. 借助tkinter设计人脸检测的界面(摄像头检测,视频检测,视频检测并保存)
  2. pandas使用tabulate函数将pandas dataframe以类似于plsql表格的方式打印出来(printing dataframe in tabular format)
  3. MySQL优化(一):表结构优化
  4. html 元素的属性
  5. python 表单中值为空的还需要传入么_牛掰!100行Python,自动动手打造一款多国语言翻译软件...
  6. 编译器预定义宏的查看和使用
  7. JSP Unable to compile class for JSP
  8. 新职业英语计算机unit5,新职业英语2Unit5.ppt
  9. 【渝粤教育】广东开放大学 商务交际听说 形成性考核 (31)
  10. crontab导致磁盘空间满问题的解决
  11. IOS 使用 ZbarSDK 二维码扫描
  12. xposed框架android9.0,xposed仓库商店下载
  13. 启动分区不存在,使用分区工具修正
  14. 正则十八式-第三式:龙跃于渊
  15. Java代码-List集合求交集、补集(差集)
  16. 优秀的论文答辩PPT模板值得被应用
  17. docker安装_Java EE,Docker和Maven(技术提示#89)
  18. Mermaid语法绘制图表
  19. 基于NLM的插值算法
  20. Retrofit 2.0 轻松实现多文件/图片上传/Json字符串/表单

热门文章

  1. 转载牛人的ASP.NET Cookies简单应用 记住用户名和密码
  2. win10 安装 cognos 10.2.0
  3. 如何确定autosar的版本_从工程师的角度看AUTOSAR
  4. pythoninterp error_从Python Interp运行时获取“importterror:尝试使用未知父包进行相对导入”...
  5. k均值的损失函数_机器学习:手撕 cross-entropy 损失函数
  6. itext生成pdf paragraph 自动换行_reportlab高级制作多格式PDF和python的class和装饰器复习
  7. 图像局部特征(一)--概述
  8. 人脸检测(一)--综述
  9. jenkins教程_2 入门
  10. 高中电子技术——电弧抑制电路