684-Redundant Connection

kevin聰發表於2018-05-06

Description

In this problem, a tree is an undirected graph that is connected and has no cycles.

The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.

Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.


Example 1:

Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:
  1
 / \
2 - 3

Example 2:

Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
    |   |
    4 - 3

Note:

  • The size of the input 2D-array will be between 3 and 1000.
  • Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

問題描述

在這個問題中, 樹是沒有環的連通的無向圖

給定的輸入為有N個節點(值互不相同, 1, 2, …N)的樹, 其中有一條邊是多餘的。多餘的邊不是已有的邊。

圖的邊由二維陣列給出。每條邊為[u, v], 其中u < v, 表示u到v的無向邊

返回多餘的邊, 使得移除這條邊後, 剩下的圖為一個有N個節點的樹。若存在多個解, 返回二維陣列中的最後一個。
返回的邊同樣以[u, v]的形式表示, u < v.


問題分析

使用dfs或者並查集來做

若加入一條邊之前, 該邊的兩個頂點已經連通, 那麼這條邊即為結果。否則, 將這條邊新增到圖中(若以並查集來做, 就是union)


解法1(dfs)

class Solution {
    Set<Integer> seen = new HashSet();
    int MAX_EDGE_VAL = 1000;

    public int[] findRedundantConnection(int[][] edges) {
        ArrayList<Integer>[] graph = new ArrayList[MAX_EDGE_VAL + 1];
        for (int i = 0; i <= MAX_EDGE_VAL; i++) {
            graph[i] = new ArrayList();
        }
        for (int[] edge: edges) {
            seen.clear();
            if (!graph[edge[0]].isEmpty() && !graph[edge[1]].isEmpty() &&
                    dfs(graph, edge[0], edge[1])) {
                return edge;
            }
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
        }
        throw new AssertionError();
    }
    //判斷source和target是否連通
    public boolean dfs(ArrayList<Integer>[] graph, int source, int target) {
        if (!seen.contains(source)) {
            seen.add(source);
            if (source == target) return true;
            for (int nei: graph[source]) {
                if (dfs(graph, nei, target)) return true;
            }
        }
        return false;
    }

解法2(並查集)

class Solution {
  public int[] findRedundantConnection(int[][] edges) {
        // max size 1000, num from 1 to 1000
        int[] parent = new int[1001];

        for(int[] edge: edges){
            int a = find(parent, edge[0]);
            int b = find(parent, edge[1]);
            if(a == b) return edge;
            // union
            parent[b] = a;
        }

        return new int[2];
    }
    private int find(int[] parent, int f) {
        if(parent[f] == 0){
            parent[f] = f;
            return f;
        }

        while(f != parent[f]){
            // path compression
            parent[f] = parent[parent[f]];  
            f = parent[f];
        }

        return f;
    }
}

相關文章