[C++/アルゴリズム]プログラマネットワーク:BFS


#include <string>
#include <vector>
#include <queue>

using namespace std;

int ch[201];
int solution(int n, vector<vector<int>> computers) {
    int answer = 0, x;
    queue<int> Q;

    for(int i=0; i<computers.size(); i++){

        if(ch[i] == 0){
            Q.push(i);
            ch[i] = 1;
            answer++;
        }

        while(!Q.empty()){
            x = Q.front();
            Q.pop();

            for(int j=0; j<computers[x].size(); j++){
                if(computers[x][j] == 1 && ch[j] == 0){
                    Q.push(j);
                    ch[j] = 1;
                }
            }
        }
    }

    return answer;
}