当前位置:网站首页>【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;
}
边栏推荐
- Cookie learning diary 1
- Neural networks and deep learning Chapter 6: Circular neural networks reading questions
- File upload bypass summary (upload labs 21 customs clearance tutorial attached)
- Machine learning decision tree
- OWASP top 10 vulnerability Guide (2021)
- Sword finger offer 04 Search in two-dimensional array
- How to remove installed elpa package
- Neural network and deep learning Chapter 1: introduction reading questions
- Machine learning -- neural network
- 【科普】热设计基础知识:5G光器件之散热分析
猜你喜欢
Components in protective circuit
Function (error prone)
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
How can CIOs use business analysis to build business value?
Download the details and sequence of the original data access from the ENA database in EBI
A survey of automatic speech recognition (ASR) research
托管式服务网络:云原生时代的应用体系架构进化
Power management bus (pmbus)
TPG x AIDU | AI leading talent recruitment plan in progress!
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
随机推荐
程序员应该怎么学数学
level18
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)
CSDN正文自动生成目录
防护电路中的元器件
The difference between bundle, chunk and module
机器学习 --- 神经网络
函数(基本:参数,返回值)
Flutter tips: various fancy nesting of listview and pageview
Introduce Hamming distance and calculation examples
Fonction (sujette aux erreurs)
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
Machine learning -- neural network
直播预告 | 容器服务 ACK 弹性预测最佳实践
Matplotlib draws three-dimensional scatter and surface graphs
windows下Redis-cluster集群搭建
Sword finger offer 04 Search in two-dimensional array
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics