点击打开链接

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. JAVA 操作Excel工具类

    Bean转Excel对象 /* * 文件名:BeanToExcel.java */ import java.util.ArrayList; import java.util.List; import ...

  2. 《C语言基础日常笔记》

    1. 类型转换-----------------20130902 a, 浮点数(包括单精度与双精度)赋值给整型变量时,舍弃浮点数的小数部分,直接将其整数部分存放在整型变量里. b, 整型变量赋值给浮点 ...

  3. VS2015解决方案资源管理器空白,不显示内容

    解决方法: 1.先关闭vs: 2.把C:/Users/<users name>/AppData/Local/Microsoft/VisualStudio/14.0/ComponentMod ...

  4. Marshal.FreeHGlobal 方法 (IntPtr)

    释放以前从进程的非托管内存中分配的内存. 命名空间:   System.Runtime.InteropServices程序集:  mscorlib(位于 mscorlib.dll) 下面的示例演示如何 ...

  5. AFNetworking 不支持 text/plain,unacceptable content-type: text/plain

    1. 用AFNetworkingPOST传递参数(获取微博的accessToken)的时候,具体代码如下: AFHTTPSessionManager *session = [AFHTTPSession ...

  6. Python打包工具

    打包Python应用,使用工具: 1.Linux和Windows下,使用pyinstaller pyinstaller -F -w XXX.py 在当前文件夹下生成两个文件夹:build .dist ...

  7. php解决时间超过2038年

    问题 超过2038年的时间 php怎么处理? echo date('Y-m-d',2147483647); //date函数能处理的最大整数2147483647 ->2038-01-19 就是2 ...

  8. TLB与内存寻址,内存读取,虚拟内存的相关原理

    TLB(Translation Lookaside Buffer)转换检测缓冲区是一个内存管理单元,用于改进虚拟地址到物理地址转换速度的缓存. TLB是一个小的,虚拟寻址的缓存,其中每一行都保存着一个 ...

  9. code1105 过河

    dp方程很简单: f[i] = min{ f[i-j] } + stone[i] 但是数据10^9太大了,超时超空间,这样只能过30% 来自:http://blog.csdn.net/w1996070 ...

  10. 为什么都说UX / UI设计师是最佳工作?

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 你将成为永远热爱自己工作的人,做着自己喜欢的工作还能得到相应的成果和报酬,就好似在度带薪年假一般,何 ...