comments | edit_url |
---|---|
true |
一个班上有 n
个同学,其中一些彼此是朋友,另一些不是。朋友关系是可以传递的,如果 a 与 b
直接是朋友,且 b
与 c
是直接朋友,那么 a
与 c
就是间接朋友。
定义 朋友圈 就是一组直接或者间接朋友的同学集合。
给定一个 n x n
的矩阵 isConnected
表示班上的朋友关系,其中 isConnected[i][j] = 1
表示第 i
个同学和第 j
个同学是直接朋友,而 isConnected[i][j] = 0
表示二人不是直接朋友。
返回矩阵中 朋友圈的数量。
示例 1:
输入:isConnected
= [[1,1,0],[1,1,0],[0,0,1]]
输出:2
示例 2:
输入:isConnected
= [[1,0,0],[0,1,0],[0,0,1]]
输出:3
提示:
1 <= n <= 200
n == isConnected.length
n == isConnected[i].length
isConnected[i][j]
为1
或0
isConnected[i][i] == 1
isConnected[i][j] == isConnected[j][i]
注意:本题与主站 547 题相同: https://leetcode.cn/problems/number-of-provinces/
判断城市之间是否属于同一个连通分量,最后连通分量的总数即为结果。
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
def dfs(i):
vis[i] = True
for j in range(n):
if not vis[j] and isConnected[i][j]:
dfs(j)
n = len(isConnected)
vis = [False] * n
ans = 0
for i in range(n):
if not vis[i]:
dfs(i)
ans += 1
return ans
class Solution {
private int[][] isConnected;
private boolean[] vis;
private int n;
public int findCircleNum(int[][] isConnected) {
n = isConnected.length;
vis = new boolean[n];
this.isConnected = isConnected;
int ans = 0;
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
dfs(i);
++ans;
}
}
return ans;
}
private void dfs(int i) {
vis[i] = true;
for (int j = 0; j < n; ++j) {
if (!vis[j] && isConnected[i][j] == 1) {
dfs(j);
}
}
}
}
class Solution {
public:
vector<vector<int>> isConnected;
vector<bool> vis;
int n;
int findCircleNum(vector<vector<int>>& isConnected) {
n = isConnected.size();
vis.resize(n);
this->isConnected = isConnected;
int ans = 0;
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
dfs(i);
++ans;
}
}
return ans;
}
void dfs(int i) {
vis[i] = true;
for (int j = 0; j < n; ++j)
if (!vis[j] && isConnected[i][j])
dfs(j);
}
};
func findCircleNum(isConnected [][]int) int {
n := len(isConnected)
vis := make([]bool, n)
var dfs func(i int)
dfs = func(i int) {
vis[i] = true
for j := 0; j < n; j++ {
if !vis[j] && isConnected[i][j] == 1 {
dfs(j)
}
}
}
ans := 0
for i := 0; i < n; i++ {
if !vis[i] {
dfs(i)
ans++
}
}
return ans
}
class Solution {
private var isConnected: [[Int]] = []
private var visited: [Bool] = []
private var n: Int = 0
func findCircleNum(_ isConnected: [[Int]]) -> Int {
self.isConnected = isConnected
self.n = isConnected.count
self.visited = [Bool](repeating: false, count: n)
var numberOfCircles = 0
for i in 0..<n {
if !visited[i] {
dfs(i)
numberOfCircles += 1
}
}
return numberOfCircles
}
private func dfs(_ i: Int) {
visited[i] = true
for j in 0..<n {
if !visited[j] && isConnected[i][j] == 1 {
dfs(j)
}
}
}
}
模板 1——朴素并查集:
# 初始化,p存储每个点的父节点
p = list(range(n))
# 返回x的祖宗节点
def find(x):
if p[x] != x:
# 路径压缩
p[x] = find(p[x])
return p[x]
# 合并a和b所在的两个集合
p[find(a)] = find(b)
模板 2——维护 size 的并查集:
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
p = list(range(n))
size = [1] * n
# 返回x的祖宗节点
def find(x):
if p[x] != x:
# 路径压缩
p[x] = find(p[x])
return p[x]
# 合并a和b所在的两个集合
if find(a) != find(b):
size[find(b)] += size[find(a)]
p[find(a)] = find(b)
模板 3——维护到祖宗节点距离的并查集:
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
p = list(range(n))
d = [0] * n
# 返回x的祖宗节点
def find(x):
if p[x] != x:
t = find(p[x])
d[x] += d[p[x]]
p[x] = t
return p[x]
# 合并a和b所在的两个集合
p[find(a)] = find(b)
d[find(a)] = distance
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
n = len(isConnected)
p = list(range(n))
for i in range(n):
for j in range(i + 1, n):
if isConnected[i][j]:
p[find(i)] = find(j)
return sum(i == v for i, v in enumerate(p))
class Solution {
private int[] p;
public int findCircleNum(int[][] isConnected) {
int n = isConnected.length;
p = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (isConnected[i][j] == 1) {
p[find(i)] = find(j);
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (i == p[i]) {
++ans;
}
}
return ans;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
class Solution {
public:
vector<int> p;
int findCircleNum(vector<vector<int>>& isConnected) {
int n = isConnected.size();
p.resize(n);
for (int i = 0; i < n; ++i) p[i] = i;
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (isConnected[i][j])
p[find(i)] = find(j);
int ans = 0;
for (int i = 0; i < n; ++i)
if (i == p[i])
++ans;
return ans;
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};
func findCircleNum(isConnected [][]int) int {
n := len(isConnected)
p := make([]int, n)
for i := range p {
p[i] = i
}
var find func(x int) int
find = func(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if isConnected[i][j] == 1 {
p[find(i)] = find(j)
}
}
}
ans := 0
for i := range p {
if p[i] == i {
ans++
}
}
return ans
}