当前位置:网站首页>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)
}
}//合并
边栏推荐
猜你喜欢
随机推荐
在mininet中测试arp欺骗
分布式一致性协议-Paxos
Technical Selection of Message Queuing
Vert.x web 接收请求时反序列化对象 Failed to decode 如何解决?
Feign Client 超时时间配置不生效
shader入门精要2
change the available bandwidth of tcp flow dynamically in mininet
三大特殊类(String Object 包装类)与异常
tcp transparent proxy (IP_TRANSPARENT)
Oauth2.0 自定义响应值以及异常处理
EastWave:垂直腔表面激光器
mininet multihomed topology
光学好书推荐
C语言函数调用过程-汇编分析
Priority table and Ascll table
【软件测试】selenium自动化测试1
Redis 学习part one
分布式一致性协议-Gossip
嵌入式学习硬件篇------初识ARM
代码细节带来的极致体验,ShardingSphere 5.1.0 性能提升密钥









