当前位置:网站首页>Joint set search: merge intervals and ask whether two numbers are in the same set
Joint set search: merge intervals and ask whether two numbers are in the same set
2022-07-03 04:18:00 【zheng. ys】
Merge :
Path compression :
#include<iostream>
using namespace std;
const int N=100010;
int n,m;
int p[N];
// return x The ancestor node of + Path compression
// Path compression : Point each node to its ancestor node
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
int main()
{
scanf("%d%d",&n,&m);
// initialization : Point the ancestor node of each node to itself
for(int i=1;i<=n;i++)
p[i]=i;
while(m--)
{
char op[2];
int a,b;
scanf("%s%d%d",op,&a,&b);
if(op[0]=='M')// Merge nodes
p[find(a)]=find(b);
else
{
if(find(a)==find(b))
puts("Yes");
else
puts("No");
}
}
return 0;
}
边栏推荐
- 用户体验五要素
- Analysis of the reason why the server cannot connect remotely
- 重绘和回流
- Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
- Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
- [set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
- Dive Into Deep Learning——2.1数据操作&&练习
- 多板块轮动策略编写技巧----策略编写学习教材
- Practical operation of vim
- Competitive product analysis and writing
猜你喜欢
Deep dive kotlin synergy (19): flow overview
Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积
Redis persistence principle
JMeter starts from zero (III) -- simple use of regular expressions
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
Fcpx template: sweet memory electronic photo album photo display animation beautiful memory
解决bp中文乱码
Preliminary cognition of C language pointer
Pdf editing tool movavi pdfchef 2022 direct download
金仓KFS数据双向同步场景部署
随机推荐
[untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions
Appium automated testing framework
GFS分布式文件系统(光是遇见已经很美好了)
QSAR model establishment script based on pytoch and rdkit
Interface in TS
Square root of X
JS realizes lazy loading of pictures
Export of zip file
eth入门之简介
Pdf editing tool movavi pdfchef 2022 direct download
深潜Kotlin协程(二十):构建 Flow
PostgreSQL database high availability Patroni source code learning - etcd class
Basic MySQL operations
[set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
mysql字段userid逗号分开保存按userid查询
【刷题篇】多数元素(超级水王问题)
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
2022 electrician (Advanced) examination papers and electrician (Advanced) examination skills
[graduation season · aggressive technology Er] Confessions of workers