본문 바로가기
자료구조 & 알고리즘

[C++] 백준 2606 바이러스

by 학식러 2023. 7. 29.
#include <bits/stdc++.h>
using namespace std;

int n, m;
vector<int> adj[101];
int a, b;
int visited[101];
int cnt;

void dfs(int here){
	visited[here] = 1;
	cnt++;
	for(int there : adj[here]){
		if(visited[there]) continue;
		dfs(there);
	}
	return;
}

int main(){
	cin >> n;
	cin >> m;
	for(int i=0; i<m; i++){
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1);
	cout << cnt-1;
	return 0;
}

댓글