s4553711
6/18/2017 - 2:59 PM

547.cpp

class Solution {
public:
    int findCircleNum(vector<vector<int>>& M) {
        int n = M.size(), res = 0;
        vector<bool> visited(n, false);
        for(int i = 0; i < n; i++) {
            if (visited[i]) continue;
            helper(M, i, visited);
            ++res;
        }
        return res;
    }
    void helper(vector<vector<int> >& M, int i, vector<bool>& visited) {
        visited[i] = true;
        for(int j = 0; j < M.size(); j++) {
            if (!M[i][j] || visited[j]) continue;
            helper(M, j, visited);
        }
    }
};