Skip to content

Commit 71111a8

Browse files
authored
Merge pull request #51 from kkangsol/main
feat(Hansol): add /Baekjoon-11724.java
2 parents 3889ca7 + 53491a1 commit 71111a8

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Hansol/august/Boj11724.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package fisa_cote.august;
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class Boj11724 {
7+
static int N, M;
8+
static boolean[] visited;
9+
static List<Integer>[] list;
10+
11+
public static void main(String[] args) throws IOException{
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
N = Integer.parseInt(st.nextToken());
15+
M = Integer.parseInt(st.nextToken());
16+
17+
list = new ArrayList[N+1];
18+
visited = new boolean[N+1];
19+
for(int i = 1; i <= N; i++) {
20+
list[i] = new ArrayList<>();
21+
}
22+
23+
for(int i = 0; i < M; i++) {
24+
st = new StringTokenizer(br.readLine());
25+
int a = Integer.parseInt(st.nextToken());
26+
int b = Integer.parseInt(st.nextToken());
27+
list[a].add(b);
28+
list[b].add(a);
29+
}
30+
31+
int count = 0;
32+
33+
for(int i = 1; i <= N; i++) {
34+
if(!visited[i]) {
35+
count++;
36+
visited[i] = true;
37+
dfs(i);
38+
}
39+
}
40+
41+
System.out.println(count);
42+
43+
}
44+
45+
private static void dfs(int num) {
46+
for(int i : list[num]) {
47+
if(!visited[i]) {
48+
visited[i] = true;
49+
dfs(i);
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)