当前位置: 首页 > news >正文

并查集——hdu1213(入门)

传送门:How Many Tables

  • 模板代入
  • 判断几个连通分支
  • DFS亦可完成

【并查集】

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 50005;
int n,m;
int a,b,ans;
int pre[maxn];

void init()
{
	for(int i=1;i<=n;i++)
		pre[i] = i;
} 

int findPre(int x)
{
	if(pre[x] == x)	return x;
	return findPre(pre[x]);			//并没有路径压缩 
}
void unite(int x,int y)
{
	int rx = findPre(x);
	int ry = findPre(y);
	if(rx==ry)	return;
	else pre[ry] = rx;			//不是pre[y] = x; 
}

int main()
{
	int flag=0,T;
	cin>>T;
	while(T--){
		cin>>n>>m;
		init();
		ans = 0;
		for(int i=1;i<=m;i++){
			cin>>a>>b;
			unite(a,b);
		}
		//判断连通分量的数量 
		for(int i=1;i<=n;i++){		
			if(i==pre[i]){			
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

【DFS】

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
bool vis[1005],lj[1005][1005];
int n,m;
void dfs(int start)
{
    vis[start]=false;
    for(int i=1;i<=n;i++)
    {
        if(vis[i]&&lj[start][i])
        {
            vis[i]=false;
            dfs(i);
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b,cnt=0;
        memset(vis,true,sizeof(vis));
        memset(lj,false,sizeof(lj));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            lj[a][b]=lj[b][a]=true;
        }
        for(int j=1;j<=n;j++)
        {
            if(vis[j])
            {
                //printf("%d\n",j);
                cnt++;
                dfs(j);
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/xzxl/p/7338622.html

相关文章:

  • FIRST集和FOLLOW集的定义和计算方法
  • 字体自适应
  • 1011.在线视频—shell脚本系列讲座(一)shell脚本与应用示例
  • ArcGIS Server 体系结构
  • Python:pygame游戏编程之旅五(游戏界面文字处理详解)
  • HDU 5358 First One(枚举)
  • 数据库回归测试
  • SELinux深入理解
  • Android应用资源---绘制资源类型(Drawable)(五)
  • 查看 SELinux状态及关闭SELinux
  • Linux下Qt与mysql建立连接
  • poj 2192 Zipper
  • centos下查看tomcat的版本号
  • 浅谈UML中常用的几种图——用例图
  • [转]项目中Struts/Spring/Hibernate的基本流程
  • 【Linux系统编程】快速查找errno错误码信息
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • Angular 4.x 动态创建组件
  • Angular2开发踩坑系列-生产环境编译
  • const let
  • Javascript 原型链
  • JavaScript设计模式之工厂模式
  • java取消线程实例
  • Python利用正则抓取网页内容保存到本地
  • ReactNativeweexDeviceOne对比
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • Vue.js-Day01
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 今年的LC3大会没了?
  • 聚类分析——Kmeans
  • 盘点那些不知名却常用的 Git 操作
  • 深度学习在携程攻略社区的应用
  • 使用 QuickBI 搭建酷炫可视化分析
  • 由插件封装引出的一丢丢思考
  • zabbix3.2监控linux磁盘IO
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​第20课 在Android Native开发中加入新的C++类
  • ​用户画像从0到100的构建思路
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (+4)2.2UML建模图
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第2节(共同的基类)
  • (待修改)PyG安装步骤
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (亲测)设​置​m​y​e​c​l​i​p​s​e​打​开​默​认​工​作​空​间...
  • (万字长文)Spring的核心知识尽揽其中
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • ..回顾17,展望18
  • .FileZilla的使用和主动模式被动模式介绍
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET正则基础之——正则委托
  • @SentinelResource详解