/*
思路就是遍历所有人,对于每一个人,寻找他的好友,找到好友后再找这个好友的好友
,这样深度优先遍历下去,设置一个flag记录是否已经遍历了这个人。
其实dfs真正有用的是flag这个变量,因为如果m个人最多m个朋友圈,设置后flag后,相同的朋友圈会
检测到flag就会不算数。
*/
public int findCircleNum(int[][] M) {
int m = M.length;
if (m==0) return 0;
int res = 0;
boolean[] flag = new boolean[m];
for (int i = 0; i < m; i++) {
if (dfs(i,M,flag)>0)
res++;
}
return res;
}
public int dfs(int i,int[][] M,boolean[] flag){
if (flag[i]) return 0;
flag[i] = true;
int count = 1;
for (int j = 0; j < M.length; j++) {
if (i!=j&&M[i][j]==1)
count+=dfs(j,M,flag);
}
return count;
}

[LeetCode]547. Friend Circles朋友圈数量--不相邻子图问题的更多相关文章

  1. [LeetCode] 547. Friend Circles 朋友圈

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  2. LeetCode 547. Friend Circles 朋友圈(C++/Java)

    题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...

  3. 547 Friend Circles 朋友圈

    班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...

  4. [LeetCode] Friend Circles 朋友圈

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  5. 547. Friend Circles 求间接朋友形成的朋友圈数量

    [抄题]: There are N students in a class. Some of them are friends, while some are not. Their friendshi ...

  6. Leetcode547: Friend Circles 朋友圈问题

    问题描述 在一个班级里有N个同学, 有些同学是朋友,有些不是.他们之间的友谊是可以传递的比如A和B是朋友,B和C是朋友,那么A和C也是朋友.我们定义 friend circle为由直接或者间接都是朋友 ...

  7. C++之路进阶——codevs2366(朋友圈)

    2366 朋友圈 2012年省队选拔赛河北  时间限制: 10 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 在很久很久以前,曾经 ...

  8. 【BZOJ 2744 】[HEOI2012]朋友圈

    Description 在很久很久以前,曾经有两个国家和睦相处,无忧无虑的生活着.一年一度的评比大会开始了,作为和平的两国,一个朋友圈数量最多的永远都是最值得他人的尊敬,所以现在就是需要你求朋友圈的最 ...

  9. bzoj 2744: [HEOI2012]朋友圈 二分图匹配

    2744: [HEOI2012]朋友圈 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 612  Solved: 174[Submit][Status] ...

随机推荐

  1. web自动化-绕过登录

    两个方法: 1.常用的方法: 第一种方法是登录后查看网站的 cookie,请求 url 的时候把 cookie 带上(缺点是:cookie有时间限制.优点:简单,方便) 2.添加cookies的方式: ...

  2. Android面试题《思考与解答》11月刊

    又来更新啦,Android面试题<思考与解答>11月刊奉上. 说说View/ViewGroup的绘制流程 View的绘制流程是从ViewRoot的performTraversals开始的, ...

  3. 适合 JS 新手学习的开源项目——在 GitHub 学编程

    作者:HelloGitHub-小鱼干 这里是 HelloGitHub 的<GitHub 上适合新手的开源项目>系列的最后一篇,系列文章: C++ 篇 Python 篇 Go 篇 Java ...

  4. 解决:com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

    com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serve ...

  5. AtomicInteger的addAndGet(int delta)与getAndAdd(int delta)有什么区别?

    结论:区别仅在于返回的结果,修改的值是相同的,但是返回的值不同. 看一下源码注释 1 /** 2 * Atomically adds the given value to the current va ...

  6. 你说说对Java中SPI的理解吧

    前言 最近在面试的时候被问到SPI了,没回答上来,主要也是自己的原因,把自己给带沟里去了,因为讲到了类加载器的双亲委派模型,后面就被问到了有哪些是破坏了双亲委派模型的场景,然后我就说到了SPI,JND ...

  7. 第12.5节 Python time模块导览

    一.时间相关的概念 time模块模块提供了各种时间相关的函数,在介绍时间相关功能前,先介绍一些术语和惯例: epoch 是时间开始的点,并且取决于平台.对于Unix, epoch 是1970年1月1日 ...

  8. pycharm执行报错: unprintable file name [Errno 2] No such file

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 老猿在pycharm执行一个工程文件testListView时,发现其工程文件对应的py文件没有后缀 ...

  9. Nessus破解没有Scan选项的解决办法

    如图,安装之后无Scan选项,流程按照吾爱破解上的文章:https://www.52pojie.cn/thread-1140341-1-1.html 解决办法为: 首先nessus.license在安 ...

  10. instanceof constructor Object.prototype.tostring.call ( [] )区别 数组和 对象的3中方法