-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |