cgy12306

[백준 BoJ] 2606 - 바이러스 본문

Algorithm/C++

[백준 BoJ] 2606 - 바이러스

cgy12306 2021. 6. 29. 16:33
// https://www.acmicpc.net/problem/2606
// 바이러스
// reference by wizely

#include<iostream>
#include<vector>
using namespace std;

void dfs(vector<vector<int>> &edge, vector<bool> &infested, int start) {

	infested[start] = true;

	for (auto next : edge[start]) {
		if (!infested[next]) {
			dfs(edge, infested, next);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int V, E, from, to, cnt = 0;
	cin >> V >> E;
	vector<vector<int>> edge(V + 1);
	vector<bool> infested(V + 1, false);

	for (int i = 0; i < E; i++) {
		cin >> from >> to;
		edge[from].push_back(to);
		edge[to].push_back(from);
	}

	dfs(edge, infested, 1);
	
	for (int v = 1; v <= V; v++) {
		if (infested[v]) cnt++;
	}
	cout << cnt - 1;
}

 

알고리즘 분류

  • DFS
  • BFS

'Algorithm > C++' 카테고리의 다른 글

[백준 BoJ] 17626 - Four Squares  (0) 2021.06.30
[백준 BoJ] 1012 - 유기농 배추  (0) 2021.06.30
[백준 BoJ] 1654 - 랜선 자르기  (0) 2021.06.29
[백준 BoJ] 2331 - 반복수열  (0) 2021.04.15
[백준 BoJ] 11724 - 연결 요소의 개수  (0) 2021.04.15
Comments