/*class unionfind{
public:
vector<int>father;
unionfind(int &num){
for(int i=0;i<num;i++)
father.push_back(i);
}
int find(int &n){
if(father[n]==n)return n;
father[n]=find(father[n]);
return father[n];//路径压缩
}
void myunion(int &a,int &b)
{
int fa=find(a);
int fb=find(b);
if(father[fb]=fa||father[fa]=fb)return;
father[fb]=fa;
}
};
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
int n=M.size();
unionfind F(n);
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(M[i][j])
F.myunion(i,j);
int res=0;
for(int i=0;i<n;i++)
if(F.find(i)==i)res++;
return res;
}*/
class Solution {
public:
int n;
vector<bool>rec;
void dfs(vector<vector<int>>& M,int i)
{
for(int j=0;j<n;j++)
if(M[i][j]&&!rec[j])
{
rec[j]=true;
dfs(M,j);
}
}
int findCircleNum(vector<vector<int>>& M) {
n=M.size();
rec=vector<bool>(n,false);
int res=0;
for(int i=0;i<n;i++)
if(!rec[i])
{
res++;
dfs(M,i);
}
return res;
}
};