Dolphins의 HelloWorld

[백준]Baekjoon11724(DFS)(대표적인 예시) 본문

Algorithm/알고리즘 개념

[백준]Baekjoon11724(DFS)(대표적인 예시)

돌핀's 2018. 8. 15. 17:51


#include <iostream>
#include <vector>

using namespace std;

vector<int> arr[1001];
bool check[1001];

void DFS(int start)
{
	check[start] = true;
	for (int i = 0; i < arr[start].size(); i++) {
		int next = arr[start][i];
		if (check[next] == false)
			DFS(next);
	}
}

int main()
{
	int N, M;
	int u, v;
	scanf("%d %d", &N, &M);

	while (M--) {
		scanf("%d %d", &u, &v);
		arr[u].push_back(v);
		arr[v].push_back(u);
	}
	int count = 0;
	for (int i = 1; i <= N; i++) {
		if (check[i] == false) {
			DFS(i);
			count++;
		}
	}
	printf("%d\n", count);
}
Comments