当前位置:网站首页>AcWing 836. Merge set (merge set)
AcWing 836. Merge set (merge set)
2022-04-23 05:33:00 【Ice Cream~】
Original link :https://www.acwing.com/problem/content/838/
subject :
Altogether n Number , The number is 1∼n, At first, each number is in a set .
Now it's time to m Operations , There are two kinds of operations :
M a b
, Will be numbered as a and b Merge the set of two numbers of , If two numbers are already in the same set , Ignore this operation ;Q a b
, The inquiry number is a and b Whether the two numbers of are in the same set ;Input format
Enter the integer in the first line n and m.
Next m That's ok , Each line contains an operation instruction , Instructions for
M a b
orQ a b
One of the .Output format
For each inquiry instruction
Q a b
, You have to output a result , If a and b Within the same set , The outputYes
, Otherwise outputNo
.Each result takes up one line .
Data range
1≤n,m≤10^5
sample input :
4 5 M 1 2 M 3 4 Q 1 2 Q 1 3 Q 3 4
sample output :
Yes No Yes
Interpretation of parallel search algorithm :
Code implementation :
#include<iostream>
using namespace std;
int n,m;
const int N=100010;
int p[N];
int find(int x)// look for x Our ancestors 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;
while(m--)
{
char op;
int a,b;
cin>>op>>a>>b;
if(op=='M')
{
p[find(a)]=find(b);//a The ancestor's parent node is equal to b Our ancestors
}
else if(op=='Q'){
if(find(a)==find(b))cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
版权声明
本文为[Ice Cream~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534524766.html
边栏推荐
- Create a tabbar component under the components folder, which is public
- Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
- Frequently asked interview questions - 3 (operating system)
- Wbpack configuring production development environment
- C, class library
- 跨域CORS的情缘~
- 巴普洛夫与兴趣爱好
- Reading notes of modern methods of C language programming
- shell指令学习1
- Deep learning object detection
猜你喜欢
After NPM was upgraded, there was a lot of panic
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
es6数组的使用
SQL Server检索SQL和用户信息的需求
The title bar will be pushed to coincide with the status bar
弘玑微课堂 | Cyclone RPA之“灵活的数字员工”执行器
Create process memory management copy_ Mm - processes and threads (IX)
Double click The jar package cannot run the solution
相机成像+单应性变换+相机标定+立体校正
OSI层常用协议
随机推荐
QSS, qdateedit, qcalendarwidget custom settings
Data mining -- understanding data
CMake基础教程(39)pkgconfig
Add two pointers? (legal or illegal)
Various situations of data / component binding
[untitled] Notepad content writing area
Phlli in a VM node
合约锁仓漏洞
shell指令学习1
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
SQL语句简单优化
Pytorch deep learning practice_ 11 convolutional neural network
STL function library
Deep learning object detection
可执行程序执行流程
selenium預先加載cookie的必要性
solidity合约DOS攻击
Vscode settings JSON configuration
What financial products will benefit during May Day?
Reading notes of modern methods of C language programming