当前位置:网站首页>【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;
}
边栏推荐
- 机器学习 --- 决策树
- 可观测|时序数据降采样在Prometheus实践复盘
- Neural networks and deep learning Chapter 3: linear model reading questions
- Neural networks and deep learning Chapter 6: Circular neural networks reading questions
- JMeter -- distributed pressure measurement
- How to carry out "small step reconstruction"?
- American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
- Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
- Hexadecimal to octal
- Introduction to RT thread kernel (5) -- memory management
猜你喜欢
Setting up redis cluster cluster under Windows
蛇形矩阵
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
3 minutes learn to create Google account and email detailed tutorial!
2022-2028 global and Chinese FPGA prototype system Market Research Report
【科普】热设计基础知识:5G光器件之散热分析
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
可观测|时序数据降采样在Prometheus实践复盘
Label exchange experiment
SPI read / write flash principle + complete code
随机推荐
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
Power management bus (pmbus)
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)
10 programming habits that web developers should develop
Basic analysis of IIC SPI protocol
Hexadecimal to octal
[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
Managed service network: application architecture evolution in the cloud native Era
SQL set operation
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
函数(易错)
Leetcode hot topic Hot 100 day 33: "subset"
可观测|时序数据降采样在Prometheus实践复盘
Variable category (automatic, static, register, external)
PHP读取ini文件并修改内容写入
Burpsuite grabs app packets
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
Construction d'un Cluster redis sous Windows
Mode in BST (binary tree & Notes on question brushing)