[백준/c++] 2606번 : 바이러스

Eunho Bae·2022년 5월 28일
0

백준

목록 보기
32/40

문제 링크


설명

BFS 방식을 이용

코드

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int num, pairs, counts;
bool visited[101];

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

	cin >> num >> pairs;
	int from, to;
	vector<vector<int>> graph(num + 1);

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

	queue<int> q;
	q.push(1);
	visited[1] = true;

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

		for (int i = 0; i < graph[f].size(); i++)
			if (!visited[graph[f][i]])
			{
				visited[graph[f][i]] = true;
				q.push(graph[f][i]);
				counts++;
			}
	}

	cout << counts;

	return 0;
}
profile
개인 공부 정리

0개의 댓글