[ 문제 ]
[ 접근방법 ]
BFS 을 통해 초대할 사람인지를 확인한다.
[ 소스코드 ]
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m, a, b, ans;
vector<int> vec[505];
queue<pair<int, int>> que;
bool chk[505];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
while(m--){
cin >> a >> b;
vec[a].push_back(b);
vec[b].push_back(a);
}
que.push({1, 0});
chk[1] = true;
while(que.size()){
pair<int, int> qf = que.front(); que.pop();
for(auto i : vec[qf.first]){
if(!chk[i] && qf.second < 2){
que.push({i, qf.second + 1});
chk[i] = true;
ans++;
}
}
}
cout << ans;
return 0;
}
'PS' 카테고리의 다른 글
[ 백준 / C++ ] 1655 : 가운데를 말해요 (0) | 2024.11.08 |
---|---|
[ 백준 / C++ ] 2578 : 빙고 (1) | 2024.11.06 |
[ 백준 / C++ ] 2210 : 숫자판 점프 (0) | 2024.11.04 |
[ 백준 / C++ ] 2166 : 다각형의 면적 (0) | 2024.10.31 |
[ 백준 / C++ ] 2631 : 줄세우기 (0) | 2024.10.30 |