2022年1月22日 星期六

1319, Number of operations to make network connected


====
1319, Number of operations to make network connected

====
Union find
====
1. 給予n台電腦, matrix of connection
所以matrix size至少要大約等於n-1

2. 給予root(n)紀錄單元root
如果有描述的link, root[y] = x

3. 如果有相同的root
(redundant (count++

====
class Solution:
vec<int> root(n)

public makeConnected(int n, vec<vec<int>>& con){

  int count=0, cable=con.size()

  if cable < n-1
    return -1
  else
    for i-n root[i] = I
    for i-n
      int x = getRoot(con[i][0])
      int y = getRoot(con[i][1])
      if x == y
        count++
      else
        root[y] = x
      return count

int getRoot(int i)
  if root[i] == I
    return i
  return getRoot(root[i])

沒有留言:

張貼留言