문제링크 : https://www.acmicpc.net/problem/11724
#include<bits/stdc++.h>
using namespace std;
int N, M;
int a[1001][1001];
bool ch[1001] = {false,};
int cnt = 0;
void DFS(int v){
for(int i=1; i<=N; i++){
if(!ch[i] && a[v][i] == 1){
ch[i] = true;
DFS(i);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
// freopen("../input.txt","rt",stdin);
cin >> N >> M;
int ta, tb;
for(int i=1; i<=M; i++){
cin >> ta >> tb;
a[ta][tb] = 1;
a[tb][ta] = 1;
}
for(int i=1; i<=N; i++){
if( !ch[i] ){
ch[i] = true;
DFS(i);
cnt++;
}
}
cout<<cnt<<endl;
return 0;
}
연결요소라는 용어가 무엇인지 몰라 input과 output을 비교해가며 이게 무슨뜻인지 알게되었다. 역시 문제를 제대로 분석하는것이 제일 중요하다.