当前位置:网站首页>【acwing】837. Number of connected block points
【acwing】837. Number of connected block points
2022-07-05 04:41:00 【The wind is a little strong】
subject

Ideas
use size[] Store the number of each root node
Code
// The number of points in a connected block exists in the root node
#include<iostream>
using namespace std;
const int N=100005;
int siz[N]; // Store the number of points in the connected block
int p[N]; // Store the parent node
int n,m;
int find(int x){
// Find the root node + Path optimization
if(p[x]!=x){
p[x]=find(p[x]);
}
return p[x];
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
p[i]=i;
siz[i]=1; // Note that there siz[i] The initial values of are 1, instead of i
}
while (m -- ){
string op;
int x,y;
cin>>op;
if(op[0]=='C'){
cin>>x>>y;
if(find(x)==find(y)){
// If x,y It's already connected , Then there is no need to go down
continue;
}
siz[find(y)]+=siz[find(x)];// The number of each connected block exists in its root node ,y The number of trees is y+x(x For the trees connected )
p[find(x)]=find(y); // This sentence should be in siz[find(y)]+=siz[find(x)] After this sentence , Otherwise x and y The value of the root node will change
}else if(op[1]=='1'){
cin>>x>>y;
if(find(x)==find(y)){
// The root node is the same , Explain in a connected block
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}else{
cin>>y;
cout<<siz[find(y)]<<endl; // Directly return the root node of the current point size[]
}
}
return 0;
}
边栏推荐
- Neural networks and deep learning Chapter 2: machine learning overview reading questions
- [uniapp] system hot update implementation ideas
- Wenet: E2E speech recognition tool for industrial implementation
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
- Uncover the seven quirky brain circuits necessary for technology leaders
- Power management bus (pmbus)
- American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
- How can CIOs use business analysis to build business value?
- [PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
- Fonction (sujette aux erreurs)
猜你喜欢

How can CIOs use business analysis to build business value?

MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP

函數(易錯)
![[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens](/img/20/5c5550e6dabc76702f0e7ce3bef068.jpg)
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens

2022-2028 global and Chinese video coding and transcoding Market Research Report

Reading and visualization of DICOM, MHD and raw files in medical imaging

level18
![[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)](/img/1b/1fa2ebc9a6c5d271c9b39f5e508fb0.jpg)
[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)

File upload bypass summary (upload labs 21 customs clearance tutorial attached)

指针函数(基础)
随机推荐
2022-2028 global and Chinese equipment as a Service Market Research Report
Fonction (sujette aux erreurs)
Neural networks and deep learning Chapter 2: machine learning overview reading questions
Observable time series data downsampling practice in Prometheus
Setting up redis cluster cluster under Windows
包 类 包的作用域
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
Invalid bound statement (not found) in idea -- problem solving
CSDN body auto generate directory
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
Key review route of probability theory and mathematical statistics examination
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
CSDN正文自动生成目录
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
Mxnet imports various libcudarts * so、 libcuda*. So not found
A solution to the problem that variables cannot change dynamically when debugging in keil5
flutter 对象和列表
2022-2028 global and Chinese FPGA prototype system Market Research Report
The remainder operation is a hash function