当前位置:网站首页>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 borQ a bOne 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 4sample 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
- windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
- QSslSocket::connectToHostEncrypted: TLS initialization failed
- egg中的多进程模型--egg文档搬运工
- The title bar will be pushed to coincide with the status bar
- C, class library
- what is wifi6?
- Double click The jar package cannot run the solution
- Pavlov and hobbies
猜你喜欢

QT displays the specified position and size of the picture
Basic knowledge of redis

Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios

selenium预先加载cookie的必要性

Traversal array, object parent-child communication props / $emit

QSS, qdateedit, qcalendarwidget custom settings

Understand the relationship between promise async await

Arithmetic and logical operations

es6数组的使用

如果我是pm之 演出电影vr购票展示
随机推荐
Basic knowledge of redis
catkin_package到底干了什么
FileReader API file operation
After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
If I am PM's performance, movie VR ticket purchase display
String class understanding - final is immutable
Necessity of selenium preloading cookies
catkin_ What did package do
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
Various situations of data / component binding
Box collapse and margin collapse
可執行程序執行流程
转置卷积(Transposed Convolution)
Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
数据安全入门产品——数据库审计系统详解
创建进程内存管理copy_mm - 进程与线程(九)
Frequently asked interview questions - 3 (operating system)
Output string in reverse order
Common interview questions - 4 (MySQL)
How to realize adaptive layout