当前位置:网站首页>【acwing】836. Merge sets
【acwing】836. Merge sets
2022-07-05 04:41:00 【The wind is a little strong】
subject
analysis
A very classic template question of joint search set
Code
#include<iostream>
using namespace std;
const int N=100005;
int p[N];//P[N] The value of is stored as N The parent node of this number
int find(int x){
// return x The ancestor node of + Path compression
if(x!=p[x]){
// If x There is no root node , Then go straight up , And make each point finally point to the root node
p[x]=find(p[x]);
}
return p[x];
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
p[i]=i; // Let the parent node of all numbers point to itself
}
while(m--){
string op;
int a,b;
cin>>op>>a>>b;
if(op[0]=='M'){
// Merge sets
p[find(a)]=find(b); //a The parent node of the root node of =b The following nodes of , hold a As b A subtree of
}else{
// Query operation
if(find(a)==find(b)){
// If a The root node and b The root nodes of are equal , Then it belongs to the same set
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
}
return 0;
}
边栏推荐
- Introduction to RT thread kernel (5) -- memory management
- The remainder operation is a hash function
- Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
- Chapter 6 text processing tools for shell programming (awk)
- Serpentine matrix
- [groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
- A solution to the problem that variables cannot change dynamically when debugging in keil5
- American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
- 2022-2028 global and Chinese virtual data storage Market Research Report
- Sword finger offer 04 Search in two-dimensional array
猜你喜欢
C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
Key review route of probability theory and mathematical statistics examination
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
CSDN正文自动生成目录
次小生成树
托管式服务网络:云原生时代的应用体系架构进化
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
Flutter tips: various fancy nesting of listview and pageview
TPG x AIDU | AI leading talent recruitment plan in progress!
随机推荐
首席信息官如何利用业务分析构建业务价值?
如何进行「小步重构」?
Sword finger offer 04 Search in two-dimensional array
How to force activerecord to reload a class- How do I force ActiveRecord to reload a class?
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
History of web page requests
Fonction (sujette aux erreurs)
Inline built-in function
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
windows下Redis-cluster集群搭建
Observable time series data downsampling practice in Prometheus
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
Mode in BST (binary tree & Notes on question brushing)
level18
Here comes the Lantern Festival red envelope!
MacBook installation postgresql+postgis
[finebi] the process of making custom maps using finebi
Official announcement! The third cloud native programming challenge is officially launched!
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
Burpsuite grabs app packets