[BaekJoon] 11724번 - 연결 요소의 개수 [Java][C++]
[BaekJoon] 11724번 - 연결 요소의 개수 [Java][C++]
1. 문제 풀이
무방향 그래프에서 연결 요소의 개수를 구하는 문제로 BFS나 DFS를 활용해서 각 노드에 대해 방문하지 않았으면 모든 연결된 노드를 방문 처리하고 개수를 하나씩 세도 되며, 유니온 파인드 알고리즘을 활용해서 서로소 집합을 만든 후 집합의 그룹장의 개수를 세주어도 된다.
2. 코드
1. BFS [Java]
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
47
48
49
50
51
52
53
54
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i <= N; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
adj.get(u).add(v);
adj.get(v).add(u);
}
boolean[] visited = new boolean[1 + N];
int cnt = 0;
for (int node = 1; node <= N; node++) {
if (visited[node]) continue;
bfs(node, visited, adj);
cnt++;
}
System.out.println(cnt);
}
static void bfs(int start, boolean[] visited, List<List<Integer>> adj) {
Queue<Integer> q = new ArrayDeque<>();
q.offer(start);
visited[start] = true;
while (!q.isEmpty()) {
int node = q.poll();
for (int next : adj.get(node)) {
if (visited[next]) continue;
q.offer(next);
visited[next] = true;
}
}
}
}
2. DFS [Java]
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
47
48
49
50
import java.io.*;
import java.util.*;
public class Main {
static List<List<Integer>> adj;
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
adj = new ArrayList<>();
for (int i = 0; i <= N; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
adj.get(u).add(v);
adj.get(v).add(u);
}
visited = new boolean[1 + N];
int cnt = 0;
for (int node = 1; node <= N; node++) {
if (visited[node]) continue;
dfs(node);
cnt++;
}
System.out.println(cnt);
}
static void dfs(int node) {
visited[node] = true;
for (int next : adj.get(node)) {
if (visited[next]) continue;
dfs(next);
}
}
}
3. DSU [Java]
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
47
48
import java.io.*;
import java.util.*;
public class Main {
static int[] p;
static int[] make(int N) {
int[] arr = new int[1 + N];
for (int i = 1; i <= N; i++) {
arr[i] = i;
}
return arr;
}
static int find(int x) {
if (x == p[x]) return x;
return p[x] = find(p[x]);
}
static void union(int x, int y) {
p[find(y)] = find(x);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
p = make(N);
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
union(u, v);
}
int cnt = 0;
for (int node = 1; node <= N; node++) {
if (node == find(node)) cnt++;
}
System.out.println(cnt);
}
}
4. BFS [C++]
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
47
48
49
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> adj(1 + 1000);
bool visited[1 + 1000];
void bfs(int start) {
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
for (int next : adj[node]) {
if (visited[next]) continue;
q.push(next);
visited[next] = true;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int cnt = 0;
for (int node = 1; node <= n; node++) {
if (visited[node]) continue;
bfs(node);
cnt++;
}
cout << cnt;
}
5. DFS [C++]
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
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> adj(1 + 1000);
bool visited[1 + 1000];
void dfs(int node) {
visited[node] = true;
for (int next : adj[node]) {
if (visited[next]) continue;
dfs(next);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int cnt = 0;
for (int node = 1; node <= n; node++) {
if (visited[node]) continue;
dfs(node);
cnt++;
}
cout << cnt;
}
6. DSU [C++]
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
#include <bits/stdc++.h>
using namespace std;
int p[1 + 1000];
void make(int n) {
for (int i = 1; i <= n; i++) p[i] = i;
}
int find(int x) {
if (x == p[x]) return x;
return p[x] = find(p[x]);
}
void unite(int x, int y) {
p[find(y)] = find(x);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
make(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
unite(u, v);
}
int cnt = 0;
for (int node = 1; node <= n; node++) {
if (node == find(node)) cnt++;
}
cout << cnt;
}
This post is licensed under CC BY 4.0 by the author.