백준/Class3

[백준] Class3-바이러스 C++ 2606번

taene_ 2023. 10. 9. 11:33

https://www.acmicpc.net/problem/2606

 

2606번: 바이러스

첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하인 양의 정수이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍

www.acmicpc.net

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

int cnt = 0;
bool chk[101] = {};

void dfs(int x, vector<int> graph[])
{
	chk[x] = true;
	for (int i = 0; i < graph[x].size(); i++)
	{
		int y = graph[x][i];
		if (!chk[y])
		{
			cnt++;
			dfs(y, graph);
		}
	}
}

int main()
{
	int computer;	//그래프 정점
	int linkLine;	//그래프 간선의 개수
	cin >> computer >> linkLine;

	vector<int> graph[101];
	while (linkLine--)
	{
		int a, b;
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	dfs(1, graph);
	cout << cnt;

	return 0;
}

접근방법: 그래프를 연결하고 dfs 함수로 1번 컴퓨터와 연결된 컴퓨터들을 탐색해서 count를 센다.