当前位置:网站首页>【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;
}
边栏推荐
- Live broadcast preview | container service ack elasticity prediction best practice
- Data security -- 14 -- Analysis of privacy protection governance
- 2022-2028 global and Chinese video coding and transcoding Market Research Report
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- 包 类 包的作用域
- How to carry out "small step reconstruction"?
- Leetcode hot topic Hot 100 day 33: "subset"
- [groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
- [crampon game] MC tutorial - first day of survival
- level18
猜你喜欢

Wenet: E2E speech recognition tool for industrial implementation

What are the building energy-saving software

自动语音识别(ASR)研究综述

指针函数(基础)

Raki's notes on reading paper: soft gazetteers for low resource named entity recognition

American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed

托管式服务网络:云原生时代的应用体系架构进化

假设检验——《概率论与数理统计》第八章学习笔记

SPI read / write flash principle + complete code

CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
随机推荐
指针函数(基础)
Neural networks and deep learning Chapter 3: linear model reading questions
Sword finger offer 07 Rebuild binary tree
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Neural networks and deep learning Chapter 2: machine learning overview reading questions
SPI read / write flash principle + complete code
Discussion on the dimension of confrontation subspace
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
Introduction to RT thread kernel (4) -- clock management
[crampon game] MC tutorial - first day of survival
程序员应该怎么学数学
Leetcode hot topic Hot 100 day 33: "subset"
假设检验——《概率论与数理统计》第八章学习笔记
TPG x AIDU|AI领军人才招募计划进行中!
[phantom engine UE] the difference between running and starting, and the analysis of common problems
2022-2028 global and Chinese equipment as a Service Market Research Report
如何进行「小步重构」?
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
官宣!第三届云原生编程挑战赛正式启动!
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)