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);






转载于:https://www.cnblogs.com/xiejunzhao/p/8227463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值