문제
접근 방식
유니온 파인드 구현 연습
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main_1717 {
static int[] parents;
static int n,m;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
makeSet();
for(int i=0;i<m;i++) {
st = new StringTokenizer(br.readLine());
int command = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if(command == 0) {
union(a,b);
}
else {
if(findSet(a) == findSet(b)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
}
public static void makeSet() {
parents = new int[n+1];
for(int i=1;i<=n;i++) {
parents[i] = i;
}
}
public static int findSet(int a) {
if(a == parents[a]) return a;
return parents[a] = findSet(parents[a]);
}
public static boolean union(int a, int b) {
if(findSet(a) == findSet(b)) return false;
parents[findSet(b)] = findSet(a);
return true;
}
}