Union Find-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.
class Solution {
public:
int dfs(vector<vector<int>>& M, bool *visited, int ind){
if(visited[ind] == ) return ;
else{
visited[ind] = ;
int ccnum = ;
for(int j = ; j < M.size(); j++){
if(ind != j && M[ind][j] == ){
ccnum += dfs(M, visited, j);
}
}
return ccnum;
}
}
int findCircleNum(vector<vector<int>>& M) {
int size = M.size();
if(size == ) return ;
else{
bool visited[size];
memset(visited, , size);
int cnum = ;
for(int i = ; i < size; i++){
if(dfs(M, visited, i) > )
cnum++;
}
return cnum;
}
}
};
Union Find-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 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- LeetCode 547. Friend Circles 朋友圈(C++/Java)
题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...
- 547 Friend Circles 朋友圈
班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...
- [LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题
/* 思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友 ,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人. 其实dfs真正有用的是flag这个变量,因为如果 ...
- LeetCode编程训练 - 合并查找(Union Find)
Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...
- 算法与数据结构基础 - 合并查找(Union Find)
Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...
- UnionFind问题总结
UnionFind就是acm中常用的并查集... 并查集常用操作 另外补充一下STL常用操作 相关问题: 547. Friend Circles 纯裸题噢... class Solution { pu ...
随机推荐
- Asia Stock Exchanges[z]
Asia Stock Exchanges July 7, 2009 This article is to summarise the trading rules of some Asia stocke ...
- 安全运维 -- Linux服务器使用公私钥密匙证书登录
环境:Ubuntu 16 前言 黑客遍地都是,ssh/pop3/ftp等爆破工具的流行让站长的日常运维工作量大大加重.Metasplot,Bruter等工具更是针对以上协议有专门 的破解方法,有字典破 ...
- 禁用xampp的ssl功能
按照Disable SSL on XAMPP for Windows文章里讲解的步骤如下: 1 以管理员身份启动XAMPP控制面板,点击Config按钮打开httpd.conf 分别注释171,539 ...
- MEME(Motif-based sequence analysis tools)使用说明
MEME(Motif-based sequence analysis tools)使用说明 2011-05-27 ~ ADMIN MEME是用于从一堆序列中搜索功能结构域的工具.比如说当你拿到了许多C ...
- centos7 nginx 加入开机启动
设置nginx开机启动 vi /etc/rc.d/init.d/nginx #编辑启动文件添加下面内容 ############################################### ...
- 使用delphi 开发多层应用(二十四)KbmMW 的消息方式和创建WIB节点
KbmMW 中支持基于UDP的消息广播,也支持TCP/IP hub/spoke 方式,还有 基于UDP或者TCP/IP 的点对点的消息传输. 1.基于UDP的消息广播
- 521. Longest Uncommon Subsequence I
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 2018.08.20 loj#117. 有源汇有上下界最小流(模板)
传送门 这题真有意思... 先是有一个点T的我怀疑人生. 然后学大佬们封装了我的dinic就莫名其妙的过了??? 所以说锅给谁好呢? 给dinic吧... 解法就是先求出一段可行流,然后从t到s加一条 ...
- python 实现排列组合
1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍. 2.一个列表数据任意组合 2.1主要是利用自带的库 #_*_ coding:utf-8 _*_ #__author__ ...
- MATLAB中的快捷键
Ctrl + c 中止程序的运行,鼠标要点到命令窗内.