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 ...
随机推荐
- MySQL伪master+Binlog+同步【转】
MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步 :伪master+Binlog+同步(本文 ...
- MySQL主从1205报错【转】
主从报错1205 Slave SQL thread retried transaction 10 time(s) in vain, giving up. Consider raising the va ...
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决办法
♦ 未在 Java构建路径中 找到父类 "javax.servlet.http.HttpServlet" ♦ 解决办法: 项目右击 → Build Path → 右侧 Add L ...
- 利用URLConnection http协议实现webservice接口功能(附HttpUtil.java)
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...
- Android应用内嵌unity3d游戏项目
在一个现有的Android项目中嵌入unity3d项目 1.将unity3d项目导出android工程 2.将第一步导出的Android工程中assets文件夹和libs文件夹下的所有内容复制到And ...
- webstorm加载项目卡死在scanning files to index
今天用webstorm导入项目时,需要加载node-modules文件夹,导致webstorm非常卡,页面提示scanning files to index... 网上搜到办法,记录下: 说明: 在n ...
- log4j 知识点
什么是log4j? log4j 是一个帮助程序员将日志语句输出到各种输出目标的工具. log4j 包的设计使得日志语句可以保留在已发布的代码中,而不会产生高性能成本. log4j 使用分层记录器可以有 ...
- git码云上传本地项目
可参考:https://blog.csdn.net/huangfei711/article/details/69388230 .在你的项目上鼠标右击点击Git bash git config --gl ...
- flutter No material widget found textfield widgets require a material widget ancestor
Error states that TextField widgets require a Material widget ancestor. Simply wrapping your whole l ...
- maven添加镜像与常用配置
maven解压后conf文件夹有个 settings.xml 在这个文件中可以配置我们的maven 配置镜像: 找到<mirrors></mirrors>找到这个节点在节点中添 ...