当前位置:网站首页>Revocable search board
Revocable search board
2022-07-24 18:22:00 【Alloy Bunny sauce】
Revocable search set is supported “ Disconnect ” The consolidation of operations , But this undo must be done in stack order .
Due to the need to implement revocation , You need to use the stack to record all the completed connection operations . And it can't compress the path .
The board is as follows :
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
const int N = 2e5+5;
int n,fa[N],rk[N];
struct edge{int u,v,w;} stk[N]; int top = 0; // Record the stack of operations ,u and v Are the two points of connection ,w Represents before connecting u and v Equal rank or not .top It's the top of the stack
inline void init(){FOR(i,1,n) fa[i]=i, rk[i]=1;} // initialization
int find(int x){return fa[x]==x ? x : find(fa[x]);} // Recursively looking for ancestors , Note that path compression is not allowed here
bool same(int u,int v){return find(u)==find(v);} // Judge u and v Whether ancestors are equal
void merge(int u,int v){ // Connect u and v
u = find(u), v = find(v); // hold u and v Assigned to its own ancestor
if(u==v) return; // If the ancestors are the same , Do nothing
if(rk[u] > rk[v]) swap(u,v); // Adjust by rank u and v Father son relationship of , Xiao Lianda
fa[u] = v; // Connect
stk[++top] = {u, v, rk[u]==rk[v]}; // Record the connection in the stack
if(rk[u]==rk[v]) rk[v]++; // If equal rank ,v The rank of +1
}
void undone(){ // Undo the last connection
int u = stk[top].u, v = stk[top].v, w = stk[top].w;
rk[v] -= w; // If the rank before connection is equal , So now v The rank of -1
fa[u] = u; // Cancel u,v Connection between
top--;
}边栏推荐
- Alibaba /166 obtains the API instructions for all products in the store
- Common methods of string (2)
- Int8 & int8, have you ever stumbled like this?
- The collapse of margin
- Section 9 cache penetration follow Daewoo redis ------- directory posts
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- Show or hide password plaintext + password box verification information
- 线段树合并板子
- Mid year inventory | in 2022, PAAS will be upgraded again
- web渗透经验汇总ing
猜你喜欢

Three ways of redis cluster

The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered

Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)

根证书的有效期与服务器SSL证书一样长吗?

Go language interface and type

About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed

关于接口的写法 1链式判读 ?. 2方法执行 (finally)一定会执行

JumpServer的使用
![[opencv] - thresholding](/img/4e/88c8c8063de7cb10e44e76e77dbb8e.png)
[opencv] - thresholding
![[record of question brushing] 20. Valid brackets](/img/81/7edc2ff0003373fe0ab2868b1a872f.png)
[record of question brushing] 20. Valid brackets
随机推荐
File upload vulnerability -.User.ini and.Htaccess
如何向 google colab 快速上传文件
[opencv] - thresholding
The collapse of margin
6126. 设计食物评分系统
Emerging potential of interactive virtual reality technology in drug discovery
ES6 cycle filter value
数组常用方法(2)
The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
Highcharts chart and report display, export data
IO multiplexing
Pytorch的旅程二:梯度下降
Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery
2022 the latest short video de watermarking analysis API interface sharing
Three ways of redis cluster
【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单
[record of question brushing] 20. Valid brackets
颜色的13 个必备方法!
第五届数字中国建设峰会在福建福州开幕
Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it