当前位置:网站首页>【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;
}
边栏推荐
- Official announcement! The third cloud native programming challenge is officially launched!
- Fluent objects and lists
- [Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
- Mode in BST (binary tree & Notes on question brushing)
- Serpentine matrix
- Neural network and deep learning Chapter 1: introduction reading questions
- QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
- 2022-2028 global and Chinese video coding and transcoding Market Research Report
- jmeter -- 分布式压测
- What are the building energy-saving software
猜你喜欢
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
自动语音识别(ASR)研究综述
直播预告 | 容器服务 ACK 弹性预测最佳实践
Minor spanning tree
Function (error prone)
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
Label exchange experiment
Power management bus (pmbus)
线上故障突突突?如何紧急诊断、排查与恢复
【科普】热设计基础知识:5G光器件之散热分析
随机推荐
Label exchange experiment
防护电路中的元器件
包 类 包的作用域
SQL set operation
flutter 对象和列表
Interface joint commissioning test script optimization V5.0 (end)
Function template
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
Data security -- 14 -- Analysis of privacy protection governance
How to remove installed elpa package
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
Leetcode 222 number of nodes of complete binary tree
TPG x AIDU|AI领军人才招募计划进行中!
Fluent objects and lists
Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
Here comes the Lantern Festival red envelope!
The difference between bundle, chunk and module
电源管理总线 (PMBus)