https://www.acmicpc.net/problem/2606
2606번: 바이러스
첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어진다. 이어서 그 수만큼 한 줄에 한 쌍씩 네트워크 상에서 직접 연결되어 있는 컴퓨터의 번호 쌍이 주어진다.
www.acmicpc.net
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> a[101];
bool check[101];
void dfs(int start) {
check[start] = true;
for (int i = 0; i < a[start].size(); i++) {
if (!check[a[start][i]]) {
dfs(a[start][i]);
}
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
}
dfs(1);
int cnt = 0;
for (int i = 2; i <= n; i++) {
if (check[i]) {
cnt++;
}
}
cout << cnt;
return 0;
}
'algorithm > dfs,bfs' 카테고리의 다른 글
[프로그래머스] 단어 변환 (0) | 2020.08.25 |
---|---|
[백준/2667] 단지번호붙이기 (0) | 2020.04.06 |
[백준/1260] DFS와 BFS (0) | 2020.04.04 |
댓글