zoukankan      html  css  js  c++  java
  • 547. Friend Circles 朋友圈

     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.

    Example 1:

    Input: 
    [[1,1,0],
     [1,1,0],
     [0,0,1]]
    Output: 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.

    Example 2:

    Input: 
    [[1,1,0],
     [1,1,1],
     [0,1,1]]
    Output: 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.

    Note:

    1. N is in range [1,200].
    2. M[i][i] = 1 for all students.
    3. If M[i][j] = 1, then M[j][i] = 1.

    班上有N个学生。有些是朋友,有些则不是。他们的友谊本质上是传递性的。例如,如果A是B的直接朋友,B是C的直接朋友,那么A是C的间接朋友。我们定义了一个朋友圈是一群直接或间接的朋友。

    给定N * N矩阵M代表班级中学生之间的朋友关系。如果M [i] [j] = 1,那么第i和第j个学生是彼此直接的朋友,否则不是。你必须输出所有学生中的朋友圈的总数。
    1. /**
    2. * 加权并查集
    3. */
    4. class WeightedQuickUnionUF {
    5. constructor(n) {
    6. this.count = n;
    7. this.parent = [];
    8. this.parent.length = n;
    9. this.size = [];
    10. this.size.length = n;
    11. for (let i = 0; i < n; i++) {
    12. this.parent[i] = i;
    13. this.size[i] = 1;
    14. }
    15. }
    16. find(p) {
    17. while (p != this.parent[p]) {
    18. p = this.parent[p];
    19. }
    20. return p;
    21. }
    22. union(p, q) {
    23. let rootP = this.find(p);
    24. let rootQ = this.find(q);
    25. if (rootP == rootQ) return;
    26. if (this.size[rootP] < this.size[rootQ]) {
    27. this.parent[rootP] = rootQ;
    28. this.size[rootQ] += this.size[rootP];
    29. } else {
    30. this.parent[rootQ] = rootP;
    31. this.size[rootP] += this.size[rootQ];
    32. }
    33. this.count--;
    34. }
    35. connented(p, q) {
    36. this.find(p) == this.find(q);
    37. }
    38. }
    39. /**
    40. * @param {number[][]} M
    41. * @return {number}
    42. */
    43. var findCircleNum = function (M) {
    44. let uf = new WeightedQuickUnionUF(M.length)
    45. for (let i = 0; i < M.length; i++) {
    46. for (let j = 0; j < M[i].length; j++) {
    47. if (M[i][j] == 1) {
    48. uf.union(i, j);
    49. }
    50. }
    51. }
    52. return uf.count;
    53. };
    54. let m = [
    55. [1, 0, 0, 1],
    56. [0, 1, 1, 0],
    57. [0, 1, 1, 1],
    58. [1, 0, 1, 1]
    59. ];
    60. let res = findCircleNum(m);
    61. console.log(res);






  • 相关阅读:
    SQL Server 2019 安装错误
    SQL Server Cardinality Estimation 简介
    SQL Server 要避免的编程坏习惯
    SQL Server 中SET XACT_ABORT设置的作用
    SQL Server nested transaction try...catch 处理模板
    SVN迁移至gitlab
    安装单机版RabbitMQ
    校验MySQL主从数据并修复
    使用Xtrabackup对数据库进行部分备份恢复
    MySQL之Xtrabackup备份还原与binlog恢复
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/8227463.html
Copyright © 2011-2022 走看看