当前位置:网站首页>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;
}边栏推荐
猜你喜欢
![[brush questions] most elements (super water king problem)](/img/79/13a715b74bc18a4a62113de76a65f6.png)
[brush questions] most elements (super water king problem)

Feature_selection

金仓KFS数据双向同步场景部署

用户体验五要素

JS realizes lazy loading of pictures
![[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强

JS realizes the animation effect of text and pictures in the visual area

Interaction free shell programming
![[brush questions] find the number pair distance with the smallest K](/img/e1/4118e2b37b5cea0454d65b877b507f.png)
[brush questions] find the number pair distance with the smallest K
随机推荐
xrandr修改分辨率与刷新率
Deep dive kotlin synergy (19): flow overview
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
PostgreSQL database high availability Patroni source code learning - etcd class
Competitive product analysis and writing
JS realizes the animation effect of text and pictures in the visual area
Xrandr modifier la résolution et le taux de rafraîchissement
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
The latest activation free version of Omni toolbox
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
跨境电商多商户系统怎么选
一名外包仔的2022年中总结
MySQL field userid comma separated save by userid query
[dynamic programming] subsequence problem
Two points -leetcode-540 A single element in an ordered array
有监督预训练!文本生成又一探索!
Arduino application development - LCD display GIF dynamic diagram