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

即当两个节点值相同时说明已经为了一个圈,否则不可能,此时区域个数加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. 使用C#对MongoDB中的数据进行查询,改动等操作

    首先,使用的是官方提供的C#訪问组件https://github.com/mongodb/mongo-csharp-driver 然后.编译后引用MongoDB.Bson.dll及MongoDB.Dr ...

  2. 实现AJAX局部刷新以及PageMethod方法的使用

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  3. PCB设计铜铂厚度、线宽和电流关系

    以下总结了网上八种电流与线宽的关系公式,表和计算公式,虽然各不相同(大体相近),但大家可以在实际的PCB板设计中,综合考虑PCB板的大小,通过电流,选择一个合适的线宽. 一.PCB电流与线宽 PCB载 ...

  4. 部署一个class文件

    只发布一个class文件找到项目工作空间/target/class..根据项目结构找到修改的java文件编译的class文件比如RegexUtils.class使用SecureFXPortable将文 ...

  5. socket.io+angular.js+express.js做个聊天应用(四)

    接着上一篇 使用angularjs构建聊天室的client <!doctype html> <html ng-app="justChatting"> < ...

  6. Java面试题之一

    下面也将收集一些经典的java面试题,琢磨这些面试题还是非常有好处,可以弄清楚一些容易混淆的知识点,下面面试题的答案部分来自网络,有些来自自己的理解,都是自己精心归纳整理的,有问题的地方,希望大家指出 ...

  7. jvm Classload method介绍

    1,jvm Classload默认几个重要方法介绍 findClass:Finds and loads the class with the specified name from the URL s ...

  8. cocos2d-x 之Scene和Layer

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  9. 使用client对象模型回写SharePoint列表

    使用client对象模型回写SharePoint列表 client对象模型是一个有效的方式回写SharePoint列表. 1. 管理员身份打开VS,新建WPF应用程序SPWriteListApp,确保 ...

  10. NYIST 914Yougth的最大化【二分搜索/Dinkelbach算法】

    转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187637.html 题目链接:http://acm.nyist.net/JudgeOnline/p ...