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. thymeleaf学习笔记

    1.${@dict.hello().fatherName} 显示对象的属性2.${@dict.hello()[0].fatherName} 显示列表对象的属性3.<div th:object=& ...

  2. Huawei® ENSP & VRP CheatSheet

    #################### 系统命令 #################### system-view sysname display current-configuration und ...

  3. iOS:定制自适应大小的透明吐司弹框

    一.简单介绍 创建一个吐司消息的黑色透明弹框,可以根据消息长短自适应大小. 可以手动创建手动显示手动关闭,也可以手动创建自动显示自动关闭. 简单好用. 二.代码使用 .h文件 // // LiveHU ...

  4. GENet/ESPNet

    GENet(更泛化的SEnet,有带参数和不参数的模块) 原文:https://blog.csdn.net/dgyuanshaofeng/article/details/84179196 SENet之 ...

  5. Scikit-learn 概述

    https://www.leiphone.com/news/201701/ZJMTak4Y8ch3Nwd0.html

  6. android编码学习

    虽然以下博客有点老,但很清晰,有不明白的基础知识,可以来这里找找. 2015年最新Android基础入门教程目录(完结版) 1. 环境配置 Android stodio gradle配置踩过的坑 An ...

  7. MongoDB下Map-Reduce使用简单翻译及示例

    目录 Map-Reduce JavaScript 函数 Map-Reduce 行为 一个简单的测试 原文地址https://docs.mongodb.com/manual/core/map-reduc ...

  8. alpine-bash镜像制作

    alpine轻量级基于busybox的发行版,特别适合基于docker的base images. 特点: small.simple.secure 官方地址: https://alpinelinux.o ...

  9. IDEA使用笔记(九)——设置文件注释

    方式一:后写文件描述信息 1:设置——如下图所示 2:验证——创建个类试试 3:验证——结果如下 4:其他,所有注释模版中包含 #parse("File Header.java") ...

  10. spring gateway 截取response 长度缺失

    网上找到一段获取修改response的代码:https://blog.csdn.net/tianyaleixiaowu/article/details/83618037 代码如下: import or ...