当前位置:网站首页>【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;
}
边栏推荐
- Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
- 如何进行「小步重构」?
- Practice | mobile end practice
- mxnet导入报各种libcudart*.so、 libcuda*.so找不到
- [illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
- Scope of package class package
- [crampon game] MC tutorial - first day of survival
- Reading and visualization of DICOM, MHD and raw files in medical imaging
- Raki's notes on reading paper: code and named entity recognition in stackoverflow
- windows下Redis-cluster集群搭建
猜你喜欢

程序员应该怎么学数学

质量体系建设之路的分分合合

TPG x AIDU|AI领军人才招募计划进行中!

How to get the first few pieces of data of each group gracefully

函數(易錯)
![[thingsboard] how to replace the homepage logo](/img/65/5296c26f975d79d65d36c2e76e47c1.png)
[thingsboard] how to replace the homepage logo

概率论与数理统计考试重点复习路线

A survey of automatic speech recognition (ASR) research
![[finebi] the process of making custom maps using finebi](/img/3a/d638dbac6a26c37087ec9550c35e63.png)
[finebi] the process of making custom maps using finebi

可观测|时序数据降采样在Prometheus实践复盘
随机推荐
Neural networks and deep learning Chapter 3: linear model reading questions
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
PHP reads the INI file and writes the modified content
windows下Redis-cluster集群搭建
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
Label exchange experiment
Leetcode 222 number of nodes of complete binary tree
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
质量体系建设之路的分分合合
揭秘技术 Leader 必备的七大清奇脑回路
PHP读取ini文件并修改内容写入
The remainder operation is a hash function
机器学习 --- 神经网络
Web开发人员应该养成的10个编程习惯
What are the building energy-saving software
Chapter 6 text processing tools for shell programming (awk)
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
Hexadecimal to decimal
[thingsboard] how to replace the homepage logo
jmeter -- 分布式压测