当前位置:网站首页>【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;
}
边栏推荐
- Variable category (automatic, static, register, external)
- 揭秘技术 Leader 必备的七大清奇脑回路
- Wenet: E2E speech recognition tool for industrial implementation
- Mxnet imports various libcudarts * so、 libcuda*. So not found
- Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
- 可观测|时序数据降采样在Prometheus实践复盘
- Hexadecimal to decimal
- Flutter tips: various fancy nesting of listview and pageview
- [finebi] the process of making custom maps using finebi
- You Li takes you to talk about C language 7 (define constants and macros)
猜你喜欢
What are the building energy-saving software
线上故障突突突?如何紧急诊断、排查与恢复
Label exchange experiment
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
WeNet:面向工业落地的E2E语音识别工具
level17
Managed service network: application architecture evolution in the cloud native Era
防护电路中的元器件
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
随机推荐
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
Construction d'un Cluster redis sous Windows
A solution to the problem that variables cannot change dynamically when debugging in keil5
PHP reads the INI file and writes the modified content
Discussion on the dimension of confrontation subspace
Burpsuite grabs app packets
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)
User behavior collection platform
首席信息官如何利用业务分析构建业务价值?
程序员应该怎么学数学
概率论与数理统计考试重点复习路线
Data security -- 14 -- Analysis of privacy protection governance
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
CSDN正文自动生成目录
Sword finger offer 04 Search in two-dimensional array
SPI read / write flash principle + complete code
History of web page requests
托管式服务网络:云原生时代的应用体系架构进化
Invalid bound statement (not found) in idea -- problem solving
Introduce Hamming distance and calculation examples