algorithm'''problem solve

[백준]2606 - 바이러스(설명X)

JunHwa Park 2020. 4. 9. 18:11
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
#include <iostream>
using namespace std;
 
void DFS(bool** connect, bool* arr, int sizeint pos, int inf = 0) {
    for (int i = 1; i <= size; i++) {
        if (connect[pos][i] == true && arr[i] == false) {
            arr[i] = true;
            DFS(connect, arr, size, i, inf);
        }
    }
}
 
int main() {
    int T, num, cnt = 0;
    cin >> T >> num;
    bool* arr = new bool[T + 1]{ false, };
    bool** connect = new bool* [T + 1];
    for (int i = 1; i <= T; i++)
        connect[i] = new bool[T + 1]{ false, };
    for (int i = 0; i < num; i++) {
        int com1, com2;
        cin >> com1 >> com2;
        connect[com1][com2] = true;
        connect[com2][com1] = true;
    }
    DFS(connect, arr, T, 1);
    for (int i = 1; i <= T; i++)
        if (arr[i])
            cnt++;
 
    cout << --cnt << endl;
}
cs