Gidhub BE Developer

[BOJ] 11724. 연결 요소의 개수

2018-10-01
goodGid

Problem

Problem URL : 연결 요소의 개수


[1] Answer Code (18. 10. 01)

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

int n,m;
int arr[1005][1005];
int cash[1005];
int cnt;

void dfs(int idx){
    cash[idx] = 1;
    for(int i=1; i<=n; i++){
        if(arr[idx][i] == 1 && cash[i] == 0) {
            dfs(i);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    cin >> n >> m;
    
    while (m--) {
        int a,b;
        cin >> a >> b;
        arr[a][b] = arr[b][a] = 1;
    }
    
    for(int i=1; i<=n; i++){
        if( cash[i] == 0) {
            cnt ++;
            dfs(i);
        }
    }
    
    cout << cnt << endl;
    
    return 0;
}

Review


Recommend

Index