杭电 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 ...
随机推荐
- web.xml中classpath*:与classpath:的区别
classpath对应src目录,该目录下的文件会在编译后被存放到WEB-INF文件夹下的classes目录. classpath:只会到你的class路径中查找配置文件,对于多个同名的配置文件,只会 ...
- bzoj2301 [HAOI2011]Problem b【莫比乌斯反演 分块】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2301 很好的一道题.首先把每个询问转化为4个子询问,最后的结果就是这四个子询问的记过加加减减 ...
- LIS(变形) HDOJ 5489 Removed Interval
题目传送门 题意:求删掉连续L长度后的LIS 分析:记rdp[i]表示以a[i]为开始的LIS长度,用nlogn的办法,二分查找-a[i].dp[i]表示以a[i]为结尾并且删去[i-L-1, i-1 ...
- 题解报告:hdu1201(18岁生日)
2018-02-24题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1201 Problem Description Gardon的18岁生日就要到了,他 ...
- memcached随笔练习
实验环境: RHEL 6.5 (已关闭selinux,iptables) 首先部署LNMP环境,该步骤采用源码编译安装 安装Nginx-1.8.0 准备软件包:nginx-1.8.0.tar.gz 下 ...
- java.util.Properties类的介绍-配置文件的读写【-Z-】
简介:java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种. #打头的是注释行,Properties会忽略注释.允许只有key ...
- Hadoop工作流--ChainMapper/ChainReducer?(三)
不多说,直接上干货! Hadoop的ChainMapper和ChainReducer使用案例(链式处理) 什么是ChainMapper/ChainReducer?
- AJPFX关于java中可访问控制符和非访问控制符的详细总结
1.类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1 )公共类修饰符 pub ...
- 定时器、线程queue、进程池和线程池
1.定时器 指定n秒后,执行任务 from threading import Timer,current_thread import os def hello(): print("%s he ...
- hihocoder1079 离散化
思路:线段树 + 离散化. 测试用例: 3 10 1 10 1 3 6 10 实现: #include <bits/stdc++.h> using namespace std; typed ...