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. 面向对象编程技术的总结和理解(c++)

    目录树 1.继承 1.1 基类成员在派生类中的访问属性 1.2继承时导致的二义性 1.3 多基继承 2.虚函数的多态 2.1虚函数的定义 2.2派生类中可以根据需要对虚函数进行重定义 2.3 虚函数的 ...

  2. WPF双向数据绑定总结

    参考官方:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/data/data-binding-wpf 实例程序:https://files. ...

  3. CAS 单点登录【2】自定义用户验证

       基础不太熟的同学可以先去看:CAS 单点登录[1]入门 方案1:CAS默认的JDBC扩展方案: CAS自带了两种简单的通过JDBC方式验证用户的处理器. 1.QueryDatabaseAuthe ...

  4. ubuntu intel网卡驱动安装(华硕B250F GAMING主板 )

    jikexianfeng@jikexianfeng:~$ sudo sudo lspci -knn :]: Intel Corporation Device [:591f] (rev ) Subsys ...

  5. Python的虚拟机安装已经如何配置Scrapy for Mac

    时间:2018年2月21日 因为时间问题,以下笔记就粗略记录.仅作为个人笔记为用 安装virtualenv和virtualenvwrapper 如何安装的细节下面这篇也有介绍,包括如何使用切换虚拟机也 ...

  6. Effective Java 第三版——70. 对可恢复条件使用检查异常,对编程错误使用运行时异常

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  7. 【Spark深入学习 -15】Spark Streaming前奏-Kafka初体验

    ----本节内容------- 1.Kafka基础概念 1.1 出世背景 1.2 基本原理 1.2.1.前置知识 1.2.2.架构和原理 1.2.3.基本概念 1.2.4.kafka特点 2.Kafk ...

  8. Roslyn如何实现简单的代码提示

    假如需要实现一个代码编辑器,其中一个很重要的功能是实现代码提示,类似VS的代码智能提示.下面用Roslyn编译器来实现一个简单的代码提示功能. 代码提示,首先必须需要知道对象的类型信息,然后通过迭代获 ...

  9. jQuery EasyUI 详解

    EasyUI 简介 easyui 是一种基于 jQuery 的用户界面插件集合. easyui 为创建现代化,互动,JavaScript 应用程序,提供必要的功能. 使用 easyui 你不需要写很多 ...

  10. HTTP 04 web 服务器

    用单台虚拟主机实现多个域名 HTTP/1.1 允许一台 HTTP 服务器搭建多个 web 站点, 例如提供 web 托管服务的供应商, 可以用一台服务器为多位客户服务, 也可以以每位客户持有的域名运行 ...