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
 
Source
分析:并查集成环问题。。关于并查集理解了为什么能判断成环,首先并查集我们知道能够判断两个点是否属于同一个集合,并且将不属于同一个集合中的点归纳到同一个集合中去。。。

这个图有环是已知的吧,但是要用代码来判断,就很复杂,首先我们把每个点看成独立的集合{0} ,{1}, {2}, 然后规定如果两个点之间有边相连,如果这两个点不属于同一个集合,那就将他们所属的结合合并,看边0-1,直接将这两个点代表的集合合并{0, 1}, 其中让1来当父节点, 看边1-2, 它们分别属于不同的集合,合并集合之后是{1, 2},让2来当父节点,依照这种逻辑关系,0的祖先节点就是2, 然后在看边0-2,他们属于一个集合,因为他们有着共同的祖先2,
这就说明0-2之间在没有0-2这条边之前已经连通了,如果在加上这条边的话那从0到2就有两条路径可达,就说明存在一个环了。。。这就是并查集所谓的成环的实质。。。

 // /*并查集*/
// int prev[1000]; // int find(int x){//查找我的掌门
// int r=x; //委托r去找掌门
// while(prev[r]!=r){//如果r的上级不是自己(也就是说他找到的大侠不是掌门)
// r=prev[r];//r就接着找他的掌门,直到到掌门为止
// }
// return r;//掌门驾到~~~~
// } // void join(int x,int y){//联通
// int fx=find(x);
// int fy=find(y); // if(fx!=fy){
// prev[fx]=fy;
// } // }
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1e3+;
int pre[maxn];
int cnt=; int Find(int x){
int u=x;
while(u!=pre[u]){
u=pre[u];
}
int i=x,j;
while(pre[i]!=u){/*压缩路径*/
j=pre[i];
pre[i]=u;
i=j;
}
return u;
} void mix(int u,int v){
int fu=Find(u);
int fv=Find(v);
if(fu!=fv){
pre[fu]=fv;
}
else{
cnt++;
}
} int main(int argc, char const *argv[])
{
int n,m;
while(~scanf("%d %d",&n,&m)){
cnt=;
for( int i=; i<n; i++ ){/*初始化*/
pre[i]=i;
}
for( int i=; i<m; i++ ){
int a,b;
cin>>a>>b;
mix(a,b);
}
cout<<cnt<<endl;
}
return ;
}

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

  1. G - Ice_cream's world I (并查集)

    点击打开链接 ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream ...

  2. HDU(3560)成环,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3560 并查集查有几个块,修改了之前我的一个方法(用什么map),直接判断根节点的id是i的个数. 然后 ...

  3. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  4. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...

  5. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  6. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  7. hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10

    搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...

  8. 1682. Crazy Professor(并查集)

    1628 加了些数论知识  先看下剩余类的概念 一个整数被正整数n除后,余数有n种情形:0,1,2,3,…,n-1,它们彼此对模n不同余.这表明,每个整数恰与这n个整数中某一个对模n同余.这样一来,按 ...

  9. 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...

随机推荐

  1. 【Spring】Springboot监听器,启动之后初始化工作

    package com.laplace.laplace.common.starter.config; import java.io.IOException; import org.slf4j.Logg ...

  2. MultipartFile文件编码判断

    MultipartFile文件编码判断 搜索:Java 判断文件的字符集编码 https://blog.csdn.net/top_code/article/details/8891796 但是在Mul ...

  3. [Python设计模式] 第21章 计划生育——单例模式

    github地址:https://github.com/cheesezh/python_design_patterns 单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式 ...

  4. 对Faster R-CNN的理解(3)

    2.2 边框回归 边框回归使用下面的几个公式: xywh是预测值,带a的是anchor的xywh,带*的是GT Box的xywh,可以看作是anchor经过一定的变换回归到附近的GT Box.

  5. Easyui中 alert 带回调函数的 消息框

    带回调函数的 消息框: $.messager.alert({ title:'消息', msg:'电话号码 只能是数字!', icon: 'info', width: 300, top:200 , // ...

  6. Future、FutureTask实现原理浅析(源码解读)

    前言 最近一直在看JUC下面的一些东西,发现很多东西都是以前用过,但是真是到原理层面自己还是很欠缺. 刚好趁这段时间不太忙,回来了便一点点学习总结. 前言 最近一直在看JUC下面的一些东西,发现很多东 ...

  7. Android Glide 源码分析系列(待完成)

    参考:https://jekton.github.io/2018/06/08/glide-disk-cache/ 参考:https://jekton.github.io/2018/06/20/glid ...

  8. C语言截取从某位置开始指定长度子字符串方法

    c语言标准库没有截取部分字符串的函数,为啥?因为用现有函数strncpy,很容易做到! ] = {""}; "}; strncpy(dest, src, ); puts( ...

  9. 《数据结构-C语言版》(严蔚敏,吴伟民版)课本源码+习题集解析使用说明

    <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明 先附上文档归类目录: 课本源码合辑  链接☛☛☛ <数据结构>课本源码合辑 习题集全解析  链接☛☛☛  ...

  10. Asp.Net MVC上传图片

    mvc上传图片 [HttpPost] public JsonResult Upload() { ) { ) { HttpPostedFileBase file = Request.Files[]; ) ...