본문 바로가기

카테고리 없음

백준 11724

#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <string.h>
#define max 1001


using namespace std;

int n, m;
int u, v;
int now;
int nxt;
int graphsize;
int cnt = 0;



vector<int> graph[max];
bool check[max];

void mark(int i) {

	queue<int> q;

	q.push(i);

	check[i] = true;

	while (!q.empty()) {
		now = q.front();
		q.pop();

		graphsize = graph[now].size();

		for (int i = 0; i < graphsize; i++) {
			nxt = graph[now][i];
			if (!check[nxt]) {
				q.push(nxt);
				check[nxt] = true;
				
			}
		}





	}
	
}


int main() {

	
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	
	memset(check, false, sizeof(check));

	cin >> n;
	cin >> m;

	for (int i = 0; i < m; i++) {
	
	
		cin >> u;
		cin >> v;

		graph[u].push_back(v);
		graph[v].push_back(u);

	
	}

		

	for (int i = 1; i <= n; i++) {
		if (!check[i]) {
			cnt++;
			mark(i);
		
		}
	}

	cout << cnt;


}