当前位置:网站首页>【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;
}
边栏推荐
- C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
- Fonction (sujette aux erreurs)
- [thingsboard] how to replace the homepage logo
- Hexadecimal to octal
- Neural networks and deep learning Chapter 3: linear model reading questions
- Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
- Construction d'un Cluster redis sous Windows
- Serpentine matrix
- A solution to the problem that variables cannot change dynamically when debugging in keil5
- CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
猜你喜欢

Setting up redis cluster cluster under Windows
![Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar](/img/bf/fb4e85143d1461a2026c88cda4a18d.jpg)
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar

Managed service network: application architecture evolution in the cloud native Era

Key review route of probability theory and mathematical statistics examination

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

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

函数(基本:参数,返回值)
![[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)](/img/03/329adb314606f29c8a4cb2260e84c8.jpg)
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)

3 minutes learn to create Google account and email detailed tutorial!

Mxnet imports various libcudarts * so、 libcuda*. So not found
随机推荐
Key review route of probability theory and mathematical statistics examination
线上故障突突突?如何紧急诊断、排查与恢复
Flutter tips: various fancy nesting of listview and pageview
A solution to the problem that variables cannot change dynamically when debugging in keil5
Setting up redis cluster cluster under Windows
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Chapter 6 text processing tools for shell programming (awk)
Wenet: E2E speech recognition tool for industrial implementation
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)
How can CIOs use business analysis to build business value?
[finebi] the process of making custom maps using finebi
托管式服务网络:云原生时代的应用体系架构进化
English topic assignment (27)
Function (basic: parameter, return value)
Sword finger offer 07 Rebuild binary tree
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
PHP读取ini文件并修改内容写入
Leetcode hot topic Hot 100 day 33: "subset"
WeNet:面向工业落地的E2E语音识别工具
质量体系建设之路的分分合合