当前位置:网站首页>【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;
}
边栏推荐
- [groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
- Sword finger offer 04 Search in two-dimensional array
- Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
- English topic assignment (27)
- [Business Research Report] Research Report on male consumption trends in other economic times -- with download link
- 2022-2028 global and Chinese FPGA prototype system Market Research Report
- The difference between bundle, chunk and module
- File upload bypass summary (upload labs 21 customs clearance tutorial attached)
- Hexadecimal to octal
- 函数(基本:参数,返回值)
猜你喜欢

函数(易错)

次小生成树

函數(易錯)
![[crampon game] MC tutorial - first day of survival](/img/81/82034c0382f545c39bd8c15f132ec7.jpg)
[crampon game] MC tutorial - first day of survival

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)
![[finebi] the process of making custom maps using finebi](/img/3a/d638dbac6a26c37087ec9550c35e63.png)
[finebi] the process of making custom maps using finebi

取余操作是一个哈希函数

Serpentine matrix

Fonction (sujette aux erreurs)

Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
随机推荐
[thingsboard] how to replace the homepage logo
Live broadcast preview | container service ack elasticity prediction best practice
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
10 programming habits that web developers should develop
Interface joint commissioning test script optimization V5.0 (end)
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
Burpsuite grabs app packets
Raki's notes on reading paper: code and named entity recognition in stackoverflow
Special information | finance, accounting, audit - 22.1.23
次小生成树
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
直播预告 | 容器服务 ACK 弹性预测最佳实践
What are the building energy-saving software
You Li takes you to talk about C language 7 (define constants and macros)
PHP读取ini文件并修改内容写入
Scope of package class package
JMeter -- distributed pressure measurement
[crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
Network security - record web vulnerability fixes