반응형
BFS 문제 입니다.
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, u, v, c[101],cnt;
vector <int> a[101];
queue <int> q;
void BFS(int x)
{
q.push(x);
c[x] = 1;
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i = 0; i < a[x].size(); i++)
{
int y = a[x][i];
if (c[y] == 0)
{
q.push(y);
c[y] = 1;
cnt++;//체크가 안되었을 때 바이러스에 감염된 컴퓨터의 수를 구함
}
}
}
}
int main() {
cin >> n;//컴퓨터의 수
cin >> m;//연결된 간선 수
while (m--)
{
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
BFS(1);
cout << cnt;
}
'Algorithm' 카테고리의 다른 글
(C++) - 백준(BOJ) 2845번 : 파티가 끝나고 난 뒤 (0) | 2017.02.26 |
---|---|
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 9935번:문자열 폭발 답 (0) | 2017.02.26 |
(C++) - 백준(BOJ) 10799번:쇠막대기 답 (0) | 2017.02.25 |
(C++) - 백준(BOJ) 2775번 : 부녀회장이 될테야 답 (0) | 2017.02.25 |
(C++) - 백준(BOJ) 6086번 : 최대 유량 답 (0) | 2017.02.24 |