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 ...
随机推荐
- 菜刀连接webshell
中国菜刀,一个非常好用而又强大的webshell,它可不是用来切菜的做饭的道具哦,是一款专业的网站管理软件,大小只有300多KB,真是小巧实用啊!不过被不法分子利用到,就是一个黑站的利器了.我记得以前 ...
- 如何配置Notepad++的C_C++语言开发环境
相信很多人用notepad++,但把其配置成为C/C++还是需要小折腾一下的.本人在网上找了很长时间,也没有一个统一的答案,而且很多人说的方法根本不管用,而且也不够通用,所以还是自己摸索了一下,分享给 ...
- dedecms操作数据库
重要的事情说三遍:代码复制完要格式化,格式化,格式化,最好重新抄一遍,小心陷阱 一.配置数据库配置文件路径/data/common.inc.php二.连接数据库dirname(__FILE__)表示当 ...
- Jmeter中正则表达式不区分大小写进行匹配
(?i)<r i="([A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12})" ...
- 2018.08.17 洛谷P3110 [USACO14DEC]驮运(最短路)
传送门 一道sb最短路,从两个起点和终点跑一边最短路之后直接枚举两人的汇合点求最小值就行了. 代码: #include<bits/stdc++.h> #define N 40005 #de ...
- 2018.07.22 codeforces750E(线段树维护状态转移)
传送门 给出一个数字字串,给出若干个询问,询问在字串的一段区间保证出现2017" role="presentation" style="position: re ...
- C语言程序设计50例(一)(经典收藏)
[程序1]题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. # ...
- shell中$(( ))、$( )与${ }的区别
转载自:http://blog.sina.com.cn/s/blog_4da051a60102uwda.html 命令替换 在bash中,$( )与` `(反引号)都是用来作命令替换的. 命令替换与变 ...
- spring的IOC/DI功能实践
一.写在前面: 做这个Demo主要是为了能够更好的理解Spring的原理,看再多的文章,听再多的讲解最终都最好自己去实现一遍,可以将Spring的功能分块实现,最终自然比较容易将各个功能组合起来. 这 ...
- BeautifulSoup基本步骤
http://blog.csdn.net/kikaylee/article/details/56841789 ’BeautifulSoup是Python的一个库,最主要的功能就是从网页爬取我们需要的数 ...