https://leetcode.com/problems/friend-circles/description/
题目:求朋友圈的个数。
思路: 非常简单的并查集。。。
class Solution {
public:
int fat[205];
void init()
{
for(int x=0;x<205;x++) fat[x]=x;
}
int find(int x)
{
return x==fat[x]?x:fat[x]=find(fat[x]);
}
int findCircleNum(vector<vector<int>>& M) {
init();
int len=M[0].size(),sum=len;
for(int x=0;x<len;x++)
for(int y=0;y<len;y++)
if(M[x][y]==1)
{
int x1=find(x);int y1=find(y);
if(x1!=y1) fat[x1]=y1,sum--;
}
return sum;
}
};