diff --git a/java/src/boj1717/Main.java b/java/src/boj1717/Main.java new file mode 100644 index 0000000..df3244a --- /dev/null +++ b/java/src/boj1717/Main.java @@ -0,0 +1,63 @@ +package boj1717; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Main { + + private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + private static int[] parents; + + public static void main(String[] args) { + String[] nm = readLine().split(" "); + int n = Integer.parseInt(nm[0]); + int m = Integer.parseInt(nm[1]); + + parents = new int[n + 1]; + for (int i = 0; i < n + 1; i++) { + parents[i] = i; + } + + while (m-- > 0) { + String[] line = readLine().split(" "); + int command = Integer.parseInt(line[0]); + int x = Integer.parseInt(line[1]); + int y = Integer.parseInt(line[2]); + + if (command == 0) { + union(x, y); + } else if (command == 1) { + if (find(x) == find(y)) { + System.out.println("YES"); + } else { + System.out.println("NO"); + } + } + } + } + + private static void union(int x, int y) { + int rootX = find(x); + int rootY = find(y); + if (rootX != rootY) { + parents[rootY] = parents[rootX]; + } + } + + private static int find(int x) { + if (parents[x] != x) { + parents[x] = find(parents[x]); + } + return parents[x]; + } + + private static String readLine() { + try { + return reader.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +}