当前位置:网站首页>【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;
}
边栏推荐
- Special information | finance, accounting, audit - 22.1.23
- 函数(基本:参数,返回值)
- [AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
- How to get the first few pieces of data of each group gracefully
- 揭秘技术 Leader 必备的七大清奇脑回路
- The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
- The remainder operation is a hash function
- Leetcode 222 number of nodes of complete binary tree
- 直播预告 | 容器服务 ACK 弹性预测最佳实践
- 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)
猜你喜欢
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
官宣!第三届云原生编程挑战赛正式启动!
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
OWASP top 10 vulnerability Guide (2021)
10 programming habits that web developers should develop
介绍汉明距离及计算示例
WeNet:面向工业落地的E2E语音识别工具
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
随机推荐
[thingsboard] how to replace the homepage logo
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
2022-2028 global and Chinese equipment as a Service Market Research Report
Minor spanning tree
English topic assignment (27)
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
SQL set operation
官宣!第三届云原生编程挑战赛正式启动!
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
How to carry out "small step reconstruction"?
level18
CSDN body auto generate directory
How to remove installed elpa package
WeNet:面向工业落地的E2E语音识别工具
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
Live broadcast preview | container service ack elasticity prediction best practice
JMeter -- distributed pressure measurement
蛇形矩阵
首席信息官如何利用业务分析构建业务价值?