杭电 2120 Ice_cream's world I (并查集求环数)
2024-09-07 23:22:38
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.
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);
}
}
最新文章
- ZOJ Problem Set - 1292 Integer Inquiry
- iOS支持Https
- [mysql] Some non-transactional changed tables couldn&#39;t be rolled back
- 将服务器返回的URL或者网址截取出来特定的字符,然后将字符返回,一般根据返回的字符判断用户是否登录等即时状态
- Android进阶笔记18:选用合适的IPC方式
- C语言中时间调用处理的相关函数介绍
- 怎么样调试正在运行的exe?
- [git] git 分支管理和工作流程
- 如何编写一个简单的makefile
- 洛谷 P1515 旅行
- CSS基础3——使用CSS格式化元素内容的字体
- MyEclipse10.0 集成 SVN
- PLSQL程序流程
- hdu3746 kmp求循环节
- Python-WXPY实现微信监控报警
- [转载] Java集合---HashMap源码剖析
- linux中 probe函数的何时调用的?
- JavaWeb之html
- rem实现移动端自适应页面
- 在linux中配置mysql的过程
热门文章
- ie下,php HTTP_REFERER获取失败的整理
- 【bzoj2084】[Poi2010]Antisymmetry
- P2955 [USACO09OCT]奇数偶数Even? Odd?
- laravel如何查找门脸及注入类方法
- java语言基础-类型运算细节
- COGS 1570. [POJ3461]乌力波
- Linux之 if命令——简单的shell文件
- Python3简明教程(十四)—— Collections模块
- exit - 使程序正常中止
- python基础一 day5 知识点