点击打开链接

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代码
 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int maxn=; int n,m;
int father[maxn]; void makeSet(){
for(int i=;i<n;i++){
father[i]=i;
}
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
makeSet();
int ans=;
int a,b;
while(m--){
scanf("%d%d",&a,&b);
int fx=findSet(a);
int fy=findSet(b);
if(fx==fy)
ans++;
else
father[fx]=fy;
}
printf("%d\n",ans);
}
return ;
}

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

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

点击打开链接

G - Ice_cream's world I (并查集)的更多相关文章

  1. G - 小希的迷宫(并查集)

    1今天准备复习三道以前做过的题呢,结果只看了一道,也因为交题的时候没把测试时候为了方便而改的数字改过来而wrong answer,浪费了好长时间,啊啊啊~~ 不过这道题应该是掌握了,嘿嘿…… Desc ...

  2. BZOJ4602:[SDOI2016]齿轮(并查集)

    Description 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x  : y.即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为v ...

  3. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  4. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  5. Ice_cream's world I(并查集成环)

    Problem Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...

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

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

  7. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  8. luogu4269 Snow Boots G (并查集)

    对于某个靴子,如果0代表某个格能走,1代表不能走,那么只要连续的1的个数的最大值>=靴子的步长,那这个靴子就不能用. 那么只要对靴子和格子都按深度排个序,然后从大到小来扫一遍(靴子越来越浅,能走 ...

  9. [USACO18FEB] Snow Boots G (离线+并查集)

    题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...

随机推荐

  1. C++ 获取特定进程的CPU使用率<转>

    C++ 获取特定进程的CPU使用率 近来发现笔记本在关闭屏幕后风扇转得特别快,打开屏幕后看任务管理器,风扇马上减速,也没有发现大量占用CPU的进程.于是想写一个小程序在后台记录每个进程的CPU使用情况 ...

  2. MobaXterm 错行,乱码

    最近使用MobaXterm ssh,发现进入ssh显示中文没问题.但如果用VIM编辑文件时如果有中文,修改插入操作有时就会有串行乱码情况,改vim编码也不行.都没有完美解决.最后发现只需要在MobaX ...

  3. IIS上架设https网站证书处理备忘

    1. 免费SSL证书申请 https://www.startssl.com 教程:http://hxs.fd.fj.cn/?action=show&id=13 2. 证书转换 申请到的证书有两 ...

  4. leetcode 27 Romove element

    描述: 删除指定元素.不是真的删除,要求把不符合的元素前移. 解决: 非常简单. int removeElement(vector<int>& nums, int val) { ) ...

  5. Python全栈工程师(Python3 所有基础内容 0-0)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图     Python一个月的基础语法 基本就到这咯    接下来是数据 ...

  6. 25- 解决'python -m pip install --upgrade pip' 报错问题

    转载于:https://blog.csdn.net/cxs123678/article/details/80659273 再安装包的时候提示 You are using pip version 9.0 ...

  7. Python深度学习之安装theano(windows)

    前方预警:windows的坑太多了,抛弃用linux吧 安装theano,提前清空自己的python环境吧,坑太多了,anaconda会自动安装path 一,首先安装python包管理anaconda ...

  8. 配置yum源方法,以及失效时的处理

    正常方法如下: step1: 备份原CentOS-Base.repo 文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...

  9. win7搭建node+npm+bower的环境

    原文的地址:https://my.oschina.net/JeeChou/blog/219699 Windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native ...

  10. linux安装mysql后root无法登录

    [root@localhost mysql]# mysql -u root -pEnter password: ERROR 1045 (28000): Access denied for user ' ...