백준 2606번 바이러스

최성현·2021년 2월 4일
0

백준 문제풀이

목록 보기
9/29

문제링크

설명

기본적인 BFS문제였으며 조심할점은 1번컴퓨터를 제외한 숫자이므로 처음에 cnt++ 을 해줄필요는없다 ! ( 문제 제대로 읽기!!!) ,

또한 링크드리스트를 이용할때와 배열을 이용할때 구분하여 잘 알아두자

소스 코드

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

using namespace std;
vector<int> graph[101];
bool visited[101];
int cnt = 0;
void bfs(int start) {
	queue<int> q;
	q.push(start);
	visited[start] = true;
	
	while (!q.empty()) {
		int a = q.front();
		q.pop();

		for (int i = 0; i < graph[a].size(); i++) {
			int b = graph[a][i];


			if (!visited[b]) {
				q.push(b);
				visited[b] = true;
				cnt++;
			}
		}

	}


	cout << cnt;
}
int main()
{
	int n;//computer수
	int k;//연결 쌍 수
	cin >> n >> k; 

	for (int i = 0; i < k; i++) {
		int a, b;
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	bfs(1);


	return 0;
}
profile
후회없이

0개의 댓글