并查集的应用,用来查找被分割的区域个数。

即当两个节点值相同时说明已经为了一个圈,否则不可能,此时区域个数加1.

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
int n,m;
int root[maxn]; int find(int a){
while(root[a]!=a){
a=root[a];
}
return a;
} int main(){
while(EOF != scanf("%d%d",&n,&m)){
for(int i=;i<n;i++){
root[i]=i;
}
int ans = ;
int a,b;
while(m--){
scanf("%d%d",&a,&b);
int ra=find(a);
int rb=find(b);
if(ra == rb)
ans++;
else
root[ra]=rb;//epual to Join
//printf("%d %d %d\n",ra,rb,ans);
}
printf("%d\n",ans);
}
return ;
}

Ice_cream's world I

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

HDOJ 2120 并查集的更多相关文章

  1. HDOJ 1272 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. HDOJ 3635 并查集- 路径压缩,带秩合并

    思路来源:http://blog.csdn.net/niushuai666/article/details/6990421 题目大意: 初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城 ...

  3. HDOJ 1325 并查集

    跟小希的迷宫基本一样,只是此题是有向图,要注意:1无环 2 只有一个入度为0的结点(根结点), 不存在入度大于1的结点.输入结束条件是两个负数,而不是-1,不然会TLE. #include<st ...

  4. HDOJ 1272 并查集 不相同父节点

    判断两点:1.任何2点的父节点不能相同->否则会导致2点间有多条通路2.所有点只有1个集合 存在一个小坑,就是第一次输入 0 0 的时候,应该输出 Yes , 否则会WA MY AC Code ...

  5. hdoj 1116 Play on Words 【并查集】+【欧拉路】

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. 并查集(HDOJ 1856)

    并查集   英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n       合并两个集合 ...

  7. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  8. 并查集 HDOJ 1232 畅通工程

    题目传送门 /* 并查集(Union-Find)裸题 并查集三个函数:初始化Init,寻找根节点Find,连通Union 考察:连通边数问题 */ #include <cstdio> #i ...

  9. 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)

    题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...

随机推荐

  1. ecshop标签大全 各个页面常用标签大全

    先从index.php主页开始 页面关键字 {$keywords } 页面标题 {$page_title} 产品分类 父分类列表 {foreach from=$categories item=cat ...

  2. c#个性化安装包

    近来想做一个模仿QQ或猎豹浏览器那样的个性化安装包,NSIS或IS等简单看了一下,比较复杂还不确定能不能实现. 想了一下,可以自己开发一个安装包程序,新建一个windows项目,但不知如何将已开发完成 ...

  3. React Native for android 项目驱动教程

    第一节 搭建开发环境 第二节 显示页面标题 第三节 实现页面布局 # React native是什么? React Native,是颠覆性的移动开发技术.它使用js开发,又是原生应用,不同于Hybri ...

  4. ACE6.2.0文件/目录操作

    文件读取.#include "ace/FILE_Connector.h"#include "ace/FILE_IO.h"void fileRW(){ACE_FI ...

  5. 「OC」@property @synthesize和id

    一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...

  6. JAVA语言的素数判断,随机数,函数调用

    近来刚学JAVA,就从JAVA写起吧,JAVA判别素数,其实方法和C/C++没什么区别,主要就是想谈一下,其中包括的3个点. (1)JAVA语言产生随机数,random函数,定义参数max的作用是给出 ...

  7. [LeetCode]题解(python):021-Merge Two Sorted Lists

    题目来源: https://leetcode.com/problems/merge-two-sorted-lists/ 题意分析: 题目给出两个排好序的链表,将这两个链表整合成一个新的有序的链表. 题 ...

  8. fork 函数的一点学习

    昨天某位少年问了我一个问题,#include<stdio.h> int main() { fork(); fork(); fork(); printf("hello " ...

  9. 什么是AOI自动光学检测机测试技术

    前言 当印刷电路板的价格以每年六到八个百分点的速度持续滑落,产品的生产良率就成为维持产业竞争力的要素.要获得高的良品率需要有良好的制造控制能力,而要获得良好的制程控制,似乎就必须要在各个制程中执行良好 ...

  10. hibernate 数据关联一对多 3.1

    一对多,多对一 (在多的一端存放一的外键) 但是在实体类中不需要创建这个外键 // 在一的一方创建Set集合 public class User { private Integer id; priva ...