UVALive 6091 并查集简单应用
来源:互联网 发布:读小说软件 编辑:程序博客网 时间:2024/06/01 09:08
点击打开链接
题意:问你给出的图中有多少颗树,树的定义与最小生成树类似,不能有重边或者环
思路:直接用并查集统计一下当前集合里的边的数量以及点的数量,如果点的数量与边的数量相等,那么是一颗树,统计完即可,水题~~~
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=510;int f[maxn],num[maxn],r[maxn];int find1(int x){ if(x!=f[x]) f[x]=find1(f[x]); return f[x];}void unite(int a,int b){ int aa=find1(a); int bb=find1(b); if(aa==bb){ num[bb]++;return ; } f[aa]=bb;num[bb]+=num[aa];r[bb]+=r[aa];}int main(){ int n,m,u,v,cas=1; while(scanf("%d%d",&n,&m)!=-1){ if(n==0&&m==0) break; for(int i=0;i<=n;i++){ f[i]=i;num[i]=1;r[i]=1; } for(int i=0;i<m;i++){ scanf("%d%d",&u,&v); unite(u,v); } int sum=0; for(int i=1;i<=n;i++){ if(find1(i)==i){ if(r[i]==num[i]) sum++; } } if(sum==0) printf("Case %d: No trees.\n",cas++); else if(sum==1) printf("Case %d: There is one tree.\n",cas++); else printf("Case %d: A forest of %d trees.\n",cas++,sum); } return 0;}
0 0
- UVALive 6091 并查集简单应用
- UVALive 6091 - Trees (并查集)
- UVALive 6091 Trees 并查集(水
- 并查集简单应用
- 并查集简单应用
- UVALive 3027---Corporative Network+并查集的应用
- UVALive 3644 X-Plosives(简单并查集)
- UVALive - 3644 并查集
- UVALive 6187 并查集
- uvalive 3644(并查集)
- uvalive 3027(并查集)
- hdu1856(并查集的简单应用)
- 简单的并查集应用食物链
- poj2236并查集简单应用
- poj1611----并查集的简单应用
- HDOJ1272 并查集简单应用
- X-Plosives +uvalive+并查集+水题
- Corporative Network +uvalive+并查集
- KNN分类算法实现By Java
- 使用WinDbg —— .NET篇 (八)
- java基础-深度克隆和浅克隆
- leetcode 13. Roman to Integer
- Android实现C语言写的系统
- UVALive 6091 并查集简单应用
- 函数定义为宏(以编译时间换空间)inline修饰的内联(内嵌)函数(空间换时间)
- 跟着沈毅学微电商(PHP):一
- 从最大似然到EM算法浅解
- 对学习java的心得体会1
- 欧拉函数--(求小于n的整数中与n互质的个数)
- Java面试基础总结
- 编程的智慧
- 【MyBatis】——快速入门