杭电 2120 Ice_cream's world I (并查集求环数)
Description
Input
Output
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

大意:有n个碉堡,m种关系每个关系表示两个碉堡连在一起,问一共有多少环。
父结点相等就有一个环。
#include<cstdio>
int n,m,fa[],i,a,b,sum;
int find(int a)
{
int r=a;
while(r != fa[r])
{
r=fa[r];
}
int i=a,j;
while(i != r)
{
j=fa[i];
fa[i]=r;
i=j;
}
return r;
}
void f1(int x,int y)
{
int nx,ny;
nx=find(x);
ny=find(y);
if(nx != ny)
{
fa[nx]=ny;
}
else
{
sum++;
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
sum=;
for(i = ; i < n ; i++)
{
fa[i]=i;
}
for(i = ; i < m ; i++)
{
scanf("%d %d",&a,&b);
f1(a,b);
}
printf("%d\n",sum);
}
}
杭电 2120 Ice_cream's world I (并查集求环数)的更多相关文章
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
- 杭电 1856 More is better (并查集求最大集合)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- hdu杭电1856 More is better【并查集】
Problem Description Mr Wang wants some boys to help him with a project. Because the project is rathe ...
- 【2020杭电多校】Total Eclipse 并查集+思维
题目链接:Total Eclipse 题意: t组输入,给你一个由n个点,m条边构成的图,每一个点的权值是ai.你每一次可以选择一批联通的点,然后让他们的权值都减去1.问最后把所有点的权值都变成0需要 ...
- 杭电 4707 pet(并查集求元素大于k的集合)
Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searche ...
- 杭电 5326 Work (并查集求子结点为k的结点数)
Description It’s an interesting experience to move from ICPC to work, end my college life and start ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- HDU 2120 Ice_cream's world I(并检查集合)
职务地址:HDU 2120 这题尽管字数不多,但就是看不懂. . 意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就能够了. 代码例如以下: #include <i ...
- hdu 2120 Ice_cream's world I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2120 Ice_cream's world I Description ice_cream's worl ...
随机推荐
- php分页例子实现读取mysql数据分页显示
以下代码是PHP分页案例,测试通过,主要是PHP+mysql实现分页,代码来处百度空间,有兴趣看的话可以了解一下PHP是如何分页的? <?php $link = mysql_connect(&q ...
- bzoj 4821 [Sdoi2017]相关分析
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...
- IOS-关闭(退)键盘事件--转
方法: 1.手势(触背景)关闭键盘 -(void)tapBackground //在ViewDidLoad中调用{ UITapGestureRecognizer * tap = [[UITapG ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- Problem E: 穷游中国在统题 优先队列 + 模拟
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=4 Problem E: 穷游中国在统题 Description Travel ...
- PowerShell~文件操作和对象遍历
ps提供了丰富的文件操作,如建立,删除,改名,移动,复制,文件夹建立,显示文件列表,同时对数组对象的遍历也很方便,如果在使用PS脚本时,希望现时传入参数,可以把参数声明为param,当然需要把它写在文 ...
- PowerShell和Bash的介绍
PowerShell是运行在windows平台的脚本,而Bash是运行在linux平台的脚本 现在bash能做的事情,PowerShell也能做,PowerShell的强大之处是它可以管理window ...
- poj3262 Protecting the Flowers
思路: 简单贪心,每次选择性价比最高的. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...
- Java报表之JFreeChart
一.JFreeChart简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications,servlets以及JSP等使用所设计. JFre ...
- mysql出错排查
1,例如:Can't connect to local MySQL server through socket '/tmp/mysql-5.5.37.sock' (2) Mysql链接出错,请配置/A ...