forked from gojung/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_7465.java
More file actions
46 lines (42 loc) · 1.5 KB
/
java_7465.java
File metadata and controls
46 lines (42 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//SWEA 7465 â¿ë ¸¶À» ¹«¸®ÀÇ °³¼ö(D4)
import java.io.*;
import java.util.*;
public class java_7465 {
static int N, M;
static int[] p;
static boolean[] visit;
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for(int t = 1; t <= T; t++){
int ctr = 0;
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
p = new int[N+1];
visit = new boolean[N+1];
for(int i = 1; i <= N; i++ ) p[i] = i;
for(int i = 0; i < M; i++){
st = new StringTokenizer(br.readLine(), " ");
union(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
for(int i = 1; i <= N; i++)
if(p[i] == i) ctr++;
sb.append("#" + t + " " + ctr + "\n");
}
System.out.print(sb);
br.close();
}
static void union(int a, int b){
a = findParent(a);
b = findParent(b);
if(a > b) p[a] = b;
else p[b] = a;
}
static int findParent(int a){
if(p[a] == a) return a;
else return findParent(p[a]);
}
}