Coding Test - cpp/BFS
[백준 5567: 결혼식] - C++
에드윈H
2021. 2. 10. 13:49
#include<iostream>
#include<queue>
#include<string.h>
#include<vector>
using namespace std;
vector<int> map[501];
bool chk[501];
int main() {
int n, m;
cin >> n >> m;
int x, y;
for (int i = 1; i <= m; i++)
{
cin >> x >> y;
map[x].push_back(y);
map[y].push_back(x);
}
queue<pair<int,int>> q;
int result = 0;
q.push({ 1,0 });
chk[1] = true;
int level = 1;
while (!q.empty())
{
int curNum = q.front().first;
int nextLevel = q.front().second+1;
if (nextLevel == 3) //친구의 친구의 친구라서 탈출
{
break;
}
q.pop();
for (int j = 0; j < map[curNum].size(); j++)
{
int cur = map[curNum][j];
if (!chk[cur])
{
chk[cur] = true;
q.push({ cur,nextLevel });
result++;
}
}
}
cout << result << endl;
return 0;
}