AcWing 836. 合并集合-Java
原题链接
简单
作者:
Susu
,
2020-01-28 23:35:09
,
所有人可见
,
阅读 1165
import java.io.*;
public class Main{
static int N = 100010;
static int[] p = new int[N];
static int m;
static int n;
static int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int n = Integer.valueOf(s[0]);
int m = Integer.valueOf(s[1]);
for(int i=1;i<=n;i++){
p[i]=i;
}
while(m-->0){
String[] strs = br.readLine().split(" ");
int a = Integer.valueOf(strs[1]);
int b = Integer.valueOf(strs[2]);
if(strs[0].equals("M")){
p[find(a)]=find(b);
}else{
if(find(a)!=find(b)) System.out.println("No");
else System.out.println("Yes");
}
}
}
}