Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Stack
- Computer science
- 구현
- 컴퓨터공학과
- 스택
- cs
- c++
- OS
- bfs
- 컴공과
- 오퍼레이팅시스템
- 개발
- coding
- 백준
- 너비우선탐색
- 그래프
- 코딩
- 컴공
- 문제풀이
- 오에스
- 알고리즘
- vector
- 브루트포스
- 정석학술정보관
- 북리뷰
- DP
- 코테
- Operating System
- 정석
- 자료구조
Archives
- Today
- Total
Little Jay
[C++] 백준 2606번 - 바이러스 본문
dfs로 간단하게 풀 수 있는 문제이다.
1번 컴퓨터를 통해 감염되는 컴퓨터의 개수를 출력하면 되기 때문에,
탐색을 할 노드를 1번 노드로 고정을 해놓고,
1번 노드를 통해 다시 재귀로 탐색을 할 때마다 감염된 컴퓨터의 수를 하나씩 증가시켜주면 된다.
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
vector<int> a[1001];
bool check[1001];
int infected = 0;
void dfs(int node) {
check[node] = true;
for (int i = 0; i < a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
infected += 1;
dfs(next);
}
}
}
void bfs(int start) {
queue<int> q;
memset(check, false, sizeof(check));
check[start] = true;
q.push(start);
while (!q.empty()) {
int node = q.front();
q.pop();
printf("%d ", node);
for (int i = 0; i < a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
check[next] = true;
q.push(next);
}
}
}
}
int main() {
int n, m;
cin >> n;
cin >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
}
dfs(1);
cout << infected << "\n";
return 0;
}'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 5430번 틀 (0) | 2021.08.20 |
|---|---|
| [C++] 백준 9093번 - 단어 뒤집기 (0) | 2021.08.19 |
| [C++] 백준 11724번 - 연결 요소의 개수 (0) | 2021.08.19 |
| [C++] 백준 1260번 - DFS와 BFS (0) | 2021.08.19 |
| [C++] 백준 1931번 - 회의실 배정 (0) | 2021.08.18 |
Comments