当前位置:网站首页>【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;
}
边栏推荐
- Invalid bound statement (not found) in idea -- problem solving
- Function overloading
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
- Practice | mobile end practice
- Machine learning decision tree
- TPG x AIDU|AI领军人才招募计划进行中!
- 【科普】热设计基础知识:5G光器件之散热分析
- windows下Redis-cluster集群搭建
- JVM 原理和流程简介
- [groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
猜你喜欢

Introduce Hamming distance and calculation examples
![[crampon game] MC tutorial - first day of survival](/img/81/82034c0382f545c39bd8c15f132ec7.jpg)
[crampon game] MC tutorial - first day of survival

level18

TPG x AIDU|AI领军人才招募计划进行中!
![[Business Research Report] Research Report on male consumption trends in other economic times -- with download link](/img/08/7ea490c46c3f64af3e78d07b19b3e3.jpg)
[Business Research Report] Research Report on male consumption trends in other economic times -- with download link
![[phantom engine UE] the difference between running and starting, and the analysis of common problems](/img/e2/49d6c4777c12e9f4e3f8b6ca6db41c.png)
[phantom engine UE] the difference between running and starting, and the analysis of common problems

介绍汉明距离及计算示例

假设检验——《概率论与数理统计》第八章学习笔记
![[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)](/img/2b/933586b6feff1d48c5bee11cd734ba.jpg)
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)

Special information | real estate and office buildings - 22.1.9
随机推荐
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)
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
Fluent objects and lists
Debug insights
What are the building energy-saving software
How to carry out "small step reconstruction"?
[Business Research Report] Research Report on male consumption trends in other economic times -- with download link
Raki's notes on reading paper: code and named entity recognition in stackoverflow
Mxnet imports various libcudarts * so、 libcuda*. So not found
A survey of automatic speech recognition (ASR) research
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
windows下Redis-cluster集群搭建
函數(易錯)
All in one 1413: determine base
Network security - record web vulnerability fixes
Introduce Hamming distance and calculation examples
Invalid bound statement (not found) in idea -- problem solving
SQL set operation
Neural networks and deep learning Chapter 2: machine learning overview reading questions
Hexadecimal to octal