문제링크
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static int N,M;
static int parents[];
static void make() {
for(int i=1;i<N;i++)
parents[i] = i;
}
static int findSet(int a) {
if(parents[a] == a) return a;
return parents[a] = findSet(parents[a]);
}
static boolean union(int a,int b) {
int aRoot = findSet(a);
int bRoot = findSet(b);
if(aRoot == bRoot) return false;
parents[bRoot] = aRoot;
return true;
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for(int t=0;t<T;t++) {
st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
parents = new int[N+1];
make();
for(int i=0;i<M;i++) {
st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if(num == 0) union(a, b);
else {
if(findSet(a) == findSet(b)) sb.append(1);
else sb.append(0);
}
}
System.out.println("#"+(t+1)+" "+sb);
}
}
}
'알고리즘 > SWExpert' 카테고리의 다른 글
[JAVA]SWEA_1263_사람네트워크2 (0) | 2021.03.26 |
---|---|
[JAVA]SWEA_7465_창용마을무리의개수 (0) | 2021.03.18 |
[JAVA]SWEA_1238_contact (0) | 2021.03.16 |
[JAVA]SWEA_1949_등산로조성 (0) | 2021.03.13 |
[JAVA]SWEA_7236_저수지의 물의 총 깊이 구하기 (0) | 2021.02.26 |