[NeetCode 150] Redundant Connection

Redundant Connection

You are given a connected undirected graph with n nodes labeled from 1 to n. Initially, it contained no cycles and consisted of n-1 edges.

We have now added one additional edge to the graph. The edge has two different vertices chosen from 1 to n, and was not an edge that previously existed in the graph.

The graph is represented as an array edges of length n where edges[i] = [ai, bi] represents an edge between nodes ai and bi in the graph.

Return an edge that can be removed so that the graph is still a connected non-cyclical graph. If there are multiple answers, return the edge that appears last in the input edges.

Example 1:

Input: edges = [[1,2],[1,3],[3,4],[2,4]]

Output: [2,4]

Example 2:

Input: edges = [[1,2],[1,3],[1,4],[3,4],[4,5]]

Output: [3,4]

Constraints:

n == edges.length
3 <= n <= 100
1 <= edges[i][0] < edges[i][1] <= edges.length

There are no repeated edges and no self-loops in the input.

Solution

If the problem does not require to return the edge that appears last in the input edges, a DFS is enough to solve this problem. If we visit a repetitive node, return the last edge we pass.

Unfortulately, we need to output the last in the input edges. A solution is Disjoint Set Union, which allows us to process the edges in input order. When a new edge is coming, we check whether the corresponding 2 vertices are already in the same set. If so, just return this edge, or we union the sets the 2 vertices belongs to.

A technique called path compression can be applied on Disjoint Set Union, which means we directly set the parent vertice of each vertice to its root vertice, saving the time of jumping along the parent path.

Code

class Solution:
    def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
        cluster = [i for i in range(0, len(edges)+2)]
        def find(a):
            if cluster[a] == a:
                return a
            cluster[a] = find(cluster[a])
            return cluster[a]
        def link(a, b):
            cluster[find(b)] = find(a)
        for edge in edges:
            if find(edge[0]) == find(edge[1]):
                return edge
            link(edge[0], edge[1])

        

相关推荐

  1. [NeetCode 150] Valid Sudoku

    2024-07-15 07:52:04       20 阅读
  2. [NeetCode 150] Word Ladder

    2024-07-15 07:52:04       23 阅读
  3. [NeetCode 150] Redundant Connection

    2024-07-15 07:52:04       26 阅读
  4. [NeetCode 150] Longest Consecutive Sequence

    2024-07-15 07:52:04       21 阅读
  5. [NeetCode 150] Products of Array Discluding Self

    2024-07-15 07:52:04       23 阅读
  6. [NeetCode 150] Merge K Sorted Linked Lists

    2024-07-15 07:52:04       26 阅读
  7. LeetCode 150, 112, 130

    2024-07-15 07:52:04       19 阅读
  8. DAY 10 | 1047, (20,150)

    2024-07-15 07:52:04       52 阅读
  9. 面试经典150题(96-100)

    2024-07-15 07:52:04       54 阅读
  10. 面试经典150题(108-110)

    2024-07-15 07:52:04       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 07:52:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 07:52:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 07:52:04       57 阅读
  4. Python语言-面向对象

    2024-07-15 07:52:04       68 阅读

热门阅读

  1. PyTorch使用细节

    2024-07-15 07:52:04       22 阅读
  2. Matplotlib库学习之figure.add_subplot函数

    2024-07-15 07:52:04       25 阅读
  3. uniapp 初始学习1

    2024-07-15 07:52:04       30 阅读
  4. 在 YAML 中的变量(使用 &和 * 定义及引用变量)

    2024-07-15 07:52:04       24 阅读
  5. Julia 交互式命令

    2024-07-15 07:52:04       24 阅读
  6. uniapp颜色选择器

    2024-07-15 07:52:04       22 阅读
  7. 什么是DDoS攻击

    2024-07-15 07:52:04       25 阅读
  8. [NeetCode 150] Word Ladder

    2024-07-15 07:52:04       23 阅读
  9. nginx+lua 实现URL重定向(根据传入的参数条件)

    2024-07-15 07:52:04       20 阅读