当前位置:网站首页>【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;
}
边栏推荐
- [uniapp] system hot update implementation ideas
- Neural network and deep learning Chapter 1: introduction reading questions
- Neural networks and deep learning Chapter 3: linear model reading questions
- 2022-2028 global and Chinese FPGA prototype system Market Research Report
- Neural networks and deep learning Chapter 2: machine learning overview reading questions
- Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
- Sword finger offer 07 Rebuild binary tree
- PR video clip (project packaging)
- Burpsuite grabs app packets
- Machine learning decision tree
猜你喜欢
Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
Power management bus (pmbus)
level18
自动语音识别(ASR)研究综述
How to get the first few pieces of data of each group gracefully
取余操作是一个哈希函数
函数(基本:参数,返回值)
Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
函数(易错)
随机推荐
计组笔记(1)——校验码、原补码乘除计算、浮点数计算
Minor spanning tree
Introduction to RT thread kernel (4) -- clock management
如何进行「小步重构」?
[phantom engine UE] the difference between running and starting, and the analysis of common problems
Chapter 6 text processing tools for shell programming (awk)
Managed service network: application architecture evolution in the cloud native Era
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
次小生成树
WeNet:面向工业落地的E2E语音识别工具
Function (basic: parameter, return value)
Solution of circular dependency
Wan broadband access technology V EPON Technology
Raki's notes on reading paper: code and named entity recognition in stackoverflow
JMeter -- distributed pressure measurement
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
windows下Redis-cluster集群搭建
Key review route of probability theory and mathematical statistics examination
The principle of attention mechanism and its application in seq2seq (bahadanau attention)