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:
- N is in range [1,200].
- M[i][i] = 1 for all students.
- If M[i][j] = 1, then M[j][i] = 1.
M[0][1] 表示0和1是直接好友关系
M[1][2] 表示1和2是直接好友关系 0,2是间接好友关系 他们是一个朋友圈
求有多少个朋友圈,也就是无向图的连通分量
C++:
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
int n = M.size() ;
int res = ;
vector<bool> visit(n , false) ;
for(int i = ; i < n ; i++){
if (!visit[i]){
dfs(i,M,visit) ;
res++ ;
}
}
return res ;
}
void dfs(int i , vector<vector<int>> M , vector<bool>& visit){
visit[i] = true ;
for(int j = ; j < M.size() ; j++){
if (M[i][j] == && !visit[j]){
dfs(j,M,visit) ;
}
}
}
};
547. Friend Circles的更多相关文章
- [LeetCode] 547. Friend Circles 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- 547. Friend Circles 求间接朋友形成的朋友圈数量
[抄题]: There are N students in a class. Some of them are friends, while some are not. Their friendshi ...
- LeetCode 547. Friend Circles 朋友圈(C++/Java)
题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...
- 【LeetCode】547. Friend Circles 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 547 Friend Circles 朋友圈
班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...
- [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode编程训练 - 合并查找(Union Find)
Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...
- UnionFind问题总结
UnionFind就是acm中常用的并查集... 并查集常用操作 另外补充一下STL常用操作 相关问题: 547. Friend Circles 纯裸题噢... class Solution { pu ...
随机推荐
- 2018 Multi-University Training Contest 9 杭电多校第九场 (有坑待补)
咕咕咕了太久 多校博客直接从第三场跳到了第九场orz 见谅见谅(会补的!) 明明最后看下来是dp场 但是硬生生被我们做成了组合数专场…… 听说jls把我们用组合数做的题都用dp来了遍 这里只放了用组 ...
- V4L2 API详解 <二> Camera详细设置【转】
转自:http://blog.sina.com.cn/s/blog_602f87700101bf36.html 作者: Sam (甄峰) sam_code@hotmail.com Camera的 ...
- linux的/etc/profile环境变量设置不生效【原创】
设置/etc/profile的java环境变量不生效 修改环境变量 /etc/profile JAVA_HOME=/opt/software/jdk1..0_25 PATH=/usr/local/sb ...
- a标签中href属性引起的页面不跳转问题
先简单描述问题,今天在做一个简单的提交页面的时候,碰到了跳转不了的问题.其中a标签的形式<a href="" onclick="submit()"> ...
- _tcsdup这个函数容易出现堆错误
#include <string.h> #include <stdio.h> #include <tchar> int main( void ) { TCHAR b ...
- JavaScript使用方法和技巧大全
有些时候你精通一门语言,但是会发现你其实整天在和其它语言打交道,也许你以为这些微不足道,不至于影响你的开发进度,但恰恰是这些你不重视的东西会浪费你很多时间,我一直以为我早在几年前就已经精通Ja ...
- 移动硬盘和u盘的区别
移动硬盘和U盘都属于便携性存储器,用于计算机之间的数据交换.但移动硬盘和U盘的区别还是非常大的,包括内部构造.容量以及整体外观上. 移动硬盘又分为机械移动硬盘和固态移动硬盘两种,主要区别在于内置的存储 ...
- hibernate框架学习第一天:hibernate介绍及基本操作
框架辅助开发者进行开发,半成品软件,开发者与框架进行合作开发 Hibernate3Hibernate是一种基于Java的轻量级的ORM框架 基于Java:底层实现是Java语言,可以脱离WEB,在纯J ...
- xtrabackup
mysqldump备份方式是采用逻辑备份,其最大的缺陷就是备份和恢复速度都慢,对于一个小于50G的数据库而言,这个速度还是能接受的,但如果数据库非常大,那再使用mysqldump备份就不太适合了.而使 ...
- springboot:接收date类型的参数
今天有个postmapping方法,地址都正确,就是死活进不去,真是奇怪了. 终于从日志中得出些端倪,见下: 只有这个属性报错,恰恰这个属性是Date型. 这句话说得更清楚: "defaul ...