当前位置:网站首页>Template series-union set
Template series-union set
2022-08-02 16:08:00 【Stubborn and cute】
并查集模板
1、初始化
void init{
for(int i=1;i<=n;i++){
fa[i]=i;//把结点iThe set number of is initialized to its own number
}
}
2、查找
版本一:
int find(int i){
while(i!=pi[i]){
i=pi[i]=pi[pi[i]];
}
return pi[i];
}
版本二:
int find(int x) {
if(x != fa[x]){
//当xNot equal to its dad(when it is an ancestor,it has no dad)
fa[x] = find(fa[x]);//Keep looking for his dad's dad
}
return fa[x];//返回祖先
}//查找
3、合并
void unity(int x, int y){
int r1 = find(x);//找到x的祖先
int r2 = find(y);//找到y的祖先
if(r1!=r2){
fa[r1] = r2;//Ancestors and ancestors become father and son(Anyone can be a father or a son)
}
}//合并
边栏推荐
猜你喜欢
LITESTAR 4D应用:室内植物照明模拟
光波导应用中的真实光栅效应
饥荒联机版Mod开发——准备工具(一)
[Inter-process communication]: pipe communication/named/unnamed
implement tcp copa on ns3
类模板/赋值运算和加等运算
Evaluate multipath BBR congestion control on ns3
px和em和rem的区别
UnityAPI-Ray-Physics
The use of a semaphore/interprocess communication 】 【 Shared memory
随机推荐
冷读123
C#实现简单的计算器
shader入门精要2
从FAST TCP到POWERTCP
audio console无法连接到RPC服务
tpproxy-tcp透明代理
MMD->Unity一站式解决方案
深入理解负载均衡
PostgreSQL 协议数据样例
使用1D-1D EPE的光波导布局设计工具
Qt | 读取文件内容并删除文件 QFile
【软件测试】项目中关于测试人员的简单介绍
你的站点可能还没有准备好用于Site KitSite Kit 无法访问 WordPress REST API。请确保其已在您的站点上启用。
Qt | 实现一个简单的可以转动的仪表盘
【软件测试】selenium自动化测试2
【网络安全】学习笔记 --02 安全通信协议
mininet hosts talk to real internet
光栅区域衍射级数和效率的规范
Apache ShardingSphere 5.1.1 正式发布
灵活的区域定义