并查集一般形式
来源:互联网 发布:虚拟机安装mac os系统 编辑:程序博客网 时间:2024/04/29 03:17
#include<stdio.h>#include<iostream>#include<cstdio>#include<string.h>#include<math.h>using namespace std;//并查集int a[10005];//一般有多少组数据数组开多大;int n,m;void chushi(){ for(int i=0;i<n;i++) a[i] = i;}int getf(int v){ if(a[v]==v) return v; else { a[v] = getf(a[v]); return a[v]; //缩短路径,方便下次查找; }}int combine(int v1,int v2){ int t1,t2; t1 = getf(v1); t2 = getf(v2);//找到t1,t2的父亲; if(t1!=t2)//如果不相等,合并; { a[t2] = t1;//定义左边的为右边的父亲; }}int main(){ int x,y; scanf("%d%d",&n,&m);
chushi(); for(int j=0;j<m;j++) { scanf("%d%d",&x,&y); combine(x,y); } int sum = 0; for(int j=0;j<m;j++)//遍历,看每个树节点的父亲是不是自己; { if(a[j]==j) { sum++; } } cout<<sum<<endl; return 0;}
阅读全文
0 0
- 并查集一般形式
- pku 2236 poj 并查集一般题
- 【并查集+平衡树启发式合并】LibreOJ β Round #2[DP一般看规律]题解
- 一般形式语义加法
- JavaScript一般定义形式
- 类的一般形式
- 类的一般形式
- 异常一般形式
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- SCOI2005 互不侵犯
- 【2017秋季校园招聘笔经面经专题汇总】
- 用例图
- 设计模式之Singleton模式(单例模式)
- 远程访问Linux的mysql时出现Access denied for user 'root'@'localhost'的问题解决
- 并查集一般形式
- C语言试题(未完)
- Copying Books UVA
- Eclipse图文安装教程
- 插入排序java实现
- 算术、’ 移位、 位操作符
- windows挂载linux网络文件系统NFS
- Map集合的四种遍历方式
- 【牛客访谈】对话何俊林:腾讯百度华为搜狗和滴滴面试题汇总