Open azl397985856 opened 1 year ago
class Solution: def makeConnected(self, n: int, connections: List[List[int]]) -> int: root = [i for i in range(n)]
def find(p):
while p != root[p]:
root[p] = root[root[p]]
p = root[p]
return p
def union(p, q):
root[find(p)] = find(q)
have = 0
for connec in connections:
a, b = connec
if find(a) != find(b):
union(a, b)
else:
have += 1
diff_root = set()
for i in range(n):
diff_root.add(find(i))
class Solution:
def makeConnected(self, n: int, connections):
if len(connections) < n - 1:
return -1
parent = list(range(n))
count = n
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
nonlocal count
rootX, rootY = find(x), find(y)
if rootX != rootY:
parent[rootX] = rootY
count -= 1
for connection in connections:
union(connection[0], connection[1])
return count - 1
并查集/dfs
class Solution:
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
root=[i for i in range(n)]
def find(p):
while p !=root[p]:
root[p]=root[root[p]]#路径压缩
p=root[p]#便于下一次循环操作
return p
def union(p,q):
root[find(p)]=find(q)
rongyu=0
for connec in connections:
a,b=connec
if find(a) !=find(b):
union(a,b)
else:
rongyu+=1#a和b处于一个集合,那么冗余的线就多一根
diff_root=set()
for i in range(n):
diff_root.add(find(i))
return len(diff_root)-1 if rongyu>=len(diff_root)-1 else -1
#缺线缆则返回-1
复杂度分析
# 并查集
# 步骤:
# 将连通块个数初始化成顶点的个数。
# 遍历所有的边 如果边上两个顶点没有连接,连接之,连通块个数-1;
class UnionFind:
def __init__(self,size):
self.father = [None] * size
self.num_of_sets = size
def find(self,x):
"""
路径压缩
"""
if self.father[x] is None: return x
self.father[x] = self.find(self.father[x])
return self.father[x]
def is_connected(self,x,y):
return self.find(x) == self.find(y)
def merge(self,x,y):
root_x,root_y = self.find(x),self.find(y)
self.father[root_x] = root_y
self.num_of_sets -= 1
class Solution:
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
if len(connections) < n-1: return -1
uf = UnionFind(n)
for n1,n2 in connections:
if uf.is_connected(n1,n2):
continue
uf.merge(n1,n2)
return uf.num_of_sets-1
class Solution {
List<Integer>[] edges;
boolean[] used;
public int makeConnected(int n, int[][] connections) {
if(connections.length < n - 1){
return -1;
}
edges = new List[n];
for(int i = 0; i<n; i++){
edges[i] = new ArrayList<Integer>();
}
for(int[] conn: connections){
edges[conn[0]].add(conn[1]);
edges[conn[1]].add(conn[0]);
}
used = new boolean[n];
int ans = 0;
for(int i = 0; i<n; i++){
if(!used[i]){
dfs(i);
ans++;
}
}
return ans - 1;
}
private void dfs(int u){
used[u] = true;
for(int v : edges[u]){
if(!used[v]){
dfs(v);
}
}
}
}
class Solution: def makeConnected(self, n: int, connections: List[List[int]]) -> int: if len(connections) < n - 1: return -1
edges = collections.defaultdict(list)
for x, y in connections:
edges[x].append(y)
edges[y].append(x)
seen = set()
def dfs(u: int):
seen.add(u)
for v in edges[u]:
if v not in seen:
dfs(v)
ans = 0
for i in range(n):
if i not in seen:
dfs(i)
ans += 1
1319. 连通网络的操作次数
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/number-of-operations-to-make-network-connected/
前置知识
暂无
题目描述