Post

[BaekJoon] 2606번 - 바이러스 [Java][C++]

[BaekJoon] 2606번 - 바이러스 [Java][C++]

문제 링크


1. 문제 풀이


바이러스를 노드, 연결 정보를 간선으로 간주한 그래프에서 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
import java.io.*;
import java.util.*;

public class Main {

    static int n;
    static boolean[][] adj;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        n = Integer.parseInt(br.readLine());
        adj = new boolean[1 + n][1 + n];

        int m = Integer.parseInt(br.readLine());
        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[u][v] = adj[v][u] = true;
        }

        System.out.println(bfs());
    }

    static int bfs() {
        Queue<Integer> q = new ArrayDeque<>();
        q.offer(1);

        boolean[] visited = new boolean[1 + n];
        visited[1] = true;

        int cnt = 0;

        while (!q.isEmpty()) {
            int node = q.poll();

            for (int next = 1; next <= n; next++) {
                if (!adj[node][next] || visited[next]) continue;

                q.offer(next);
                visited[next] = true;
                cnt++;
            }
        }

        return cnt;
    }
}


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
import java.io.*;
import java.util.*;

public class Main {

    static int n;
    static boolean[][] adj;
    static boolean[] visited;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        n = Integer.parseInt(br.readLine());
        adj = new boolean[1 + n][1 + n];
        visited = new boolean[1 + n];

        int m = Integer.parseInt(br.readLine());
        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[u][v] = adj[v][u] = true;
        }

        System.out.println(dfs(1) - 1);
    }

    static int dfs(int node) {
        visited[node] = true;
        int cnt = 1;

        for (int next = 1; next <= n; next++) {
            if (!adj[node][next] || visited[next]) continue;
            cnt += dfs(next);
        }

        return cnt;
    }
}


3. 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
#include <bits/stdc++.h>
using namespace std;

int n;
bool adj[101][101];
bool visited[101];

int bfs() {
    queue<int> q;
    q.push(1);

    visited[1] = true;

    int cnt = 0;

    while (!q.empty()) {
        int node = q.front();
        q.pop();

        for (int next = 1; next <= n; next++) {
            if (!adj[node][next] || visited[next]) continue;

            q.push(next);
            visited[next] = true;
            cnt++;
        }
    }

    return cnt;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u][v] = adj[v][u] = true;
    }

    cout << bfs();
}


4. 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
#include <bits/stdc++.h>
using namespace std;

int n;
bool adj[101][101];
bool visited[101];

int dfs(int node) {
    visited[node] = true;
    int cnt = 1;

    for (int next = 1; next <= n; next++) {
        if (!adj[node][next] || visited[next]) continue;
        cnt += dfs(next);
    }

    return cnt;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u][v] = adj[v][u] = true;
    }

    cout << dfs(1) - 1;
}

This post is licensed under CC BY 4.0 by the author.