AcWing 836. 合并集合
原题链接
简单
作者:
Acvv_scl
,
2021-03-02 00:30:26
,
所有人可见
,
阅读 288
import java.util.*;
public class Main{
static int N=100010;
static int[]p=new int[N];
static int find(int x){
return p[x]= p[x]==x?x:find(p[x]);
}
static void merge(int a,int b){
int fa=find(a);
int fb=find(b);
if(fa==fb)return ;
p[fa]=fb;
}
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<=n;i++){
p[i]=i;
}
int m=sc.nextInt();
while(m-->0){
String op=sc.next();
if(op.equals("M")){
merge(sc.nextInt(),sc.nextInt());
}else{
String res=find(sc.nextInt())==find(sc.nextInt())?"Yes":"No";
System.out.println(res);
}
}
}
}