백준 2606 - 바이러스


Tags : DFS


문제

백준 2606 - 바이러스

image


풀이

image

#include <iostream>
#include <vector>
using namespace std;

vector<int> N[101];
int virus[101];
int ans;

void DFS(int x)
{
	for (int i = 0; i < (int)N[x].size(); i++) {
		int k = N[x][i];

		if (virus[k] == 0)
		{
			virus[k] = 1;
			ans++;
			DFS(k);
		}
	}
	return;
}

int main()
{
	int n, m, a, b = 0;

	cin >> n >> m;

	for (int i = 0; i < m; ++i)
	{
		cin >> a >> b;

		N[a].push_back(b);
		N[b].push_back(a);
	}

	virus[1] = 1;
	DFS(1);

	cout << ans;

	return 0;
}