Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

차근차근

[백준 2606] 바이러스 본문

대학교/Algorithm

[백준 2606] 바이러스

SWKo 2020. 2. 26. 16:19

0. 제목

  • 백준 2606 바이러스
  • BOJ 2606 바이러스
  • C++ 2606 바이러스

1. 문제

https://www.acmicpc.net/problem/2606


2. 풀이

  • 한 컴퓨터가 웜 바이러스에 걸리면 그 컴퓨터와 네트워크 상에서 연결되어 있는 모든 컴퓨터는 웜 바이러스에 걸리게 된다.
  • dfs를 활용하여 연결되어 있는 컴퓨터들의 개수를 구한다. dfs내부에서 감염 컴퓨터 수를 세는 cnt 변수를 사용한다.
  • 1번 컴퓨터를 통해 웜 바이러스에 걸리게 되는 컴퓨터를 구하는 문제이기 때문에 dfs(1)을 수행하여 정답을 구할 수 있다.

3. 코드

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
#include <iostream>
#include <vector>
using namespace std;
 
vector<int> v[101];
int visited[101= {0};
int cnt;
 
void dfs(int x){
    if(visited[x]) return;
    visited[x] = 1;
    cnt++;
    for(int i = 0; i < v[x].size(); i++){
        int y = v[x][i];
        dfs(y);
    }
}
 
int main(int argc, const char * argv[]) {
    int N, M;
    int a, b;
    
    cin >> N >> M;
    
    for(int i = 0; i < M; i++){
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    
    dfs(1);
    cout << cnt - 1 << '\n';
    
    return 0;
}
 
 

 

'대학교 > Algorithm' 카테고리의 다른 글

[백준 1717] 집합의 표현  (0) 2020.02.26
[백준 1325] 효율적인 해킹  (0) 2020.02.26
[백준 1764] 듣보잡  (0) 2020.02.24
[백준 6581] HTML  (0) 2020.02.24
[백준 2002] 추월  (0) 2020.02.24
Comments