LeetCode算法题集-547. Friend Circles(朋友圈)

有个班级,里面有N个学生,他们之中有些是朋友有些不是,比如如果A是B的朋友,B是C的朋友,那么A就是C的间接朋友,我们定义所谓的朋友圈就是由直系和间接朋友所组成的群体。

给定一个N*N的矩阵M,代表这个班级里所有学生的朋友关系,如果M[i][j] = 1,那么第i个和第j个学生就是互为直系朋友,不为1的话就不是朋友。而你的任务就是输出整个班级里总的朋友圈数量。

英语原文:

There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.

Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.

例子 1:

输入: 
[[1,1,0],
 [1,1,0],
 [0,0,1]]
输出: 2
解释:第0个和第1个学生是直系朋友,所以记为1个朋友圈。第2个学生他没什么朋友也要算一个朋友圈,所以结果为2.
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle. 
The 2nd student himself is in a friend circle. So return 2.

例子 2:

输入: 
[[1,1,0],
 [1,1,1],
 [0,1,1]]
输出: 1
解释:第0个和第1个学生是直系朋友,第1和第2个也是,所以第0和第2个学生是间接朋友,三个学生都在同个朋友圈里,返回1.
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends, 
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.

注:

  1. N的范围为 [1,200].
  2. 所有学生自己对自己都是1,即M[i][i] = 1.
  3. 如果 M[i][j] = 1, 那么 M[j][i] = 1.

解答:
关键词是并查集,不知道的可以百度下。思路见代码注释:
class Solution {
public:
    int findCircleNum(vector>& M) {
        if (M.empty())
            return 0;
        vector pre(M.size());
        for(int i=0; i& pre)
    {
        return pre[x]==x ? x : pre[x] = find(pre[x], pre);//“pre[x] = ”这句为路径压缩,直接指向组的根节点,下次查询时就快很多了。
    }
};



你可能感兴趣的:(LeetCode,C++,算法)