LN : leetcode 684 Redundant Connection

lc 684 Redundant Connection



684 Redundant Connection

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.

Union Find Accepted

Union Find的关键思想是使各结点依次连结在一起,如果有从1到2的边,则令uni[1] = 2,如果有从2到3的边,则令uni[2] = 3,这样如果此时新加一条从1到3的边,那么从1开始寻找,uni1为2,uni[2]为3,即1已经有了一条从1到3的边,再加会导致形成环路,所以这就是我们要的答案。

class Solution {
public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        vector<int> uni(2001, -1);
        for (auto edge : edges) {
            int head = edge[0], tail = edge[1];
            int x = find(uni, head), y = find(uni, tail);
            if (x == y) return edge;
            uni[x] = y;
        }
        return {};
    }
    int find(vector<int>& uni, int num) {
        while(uni[num] != -1) {
            num = uni[num];
        }
        return num;
    }
};

原文地址:https://www.cnblogs.com/renleimlj/p/8110600.html

时间: 2024-10-10 07:46:38

LN : leetcode 684 Redundant Connection的相关文章

LeetCode 685. Redundant Connection II

原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly

684. Redundant Connection

此题可以使用两种思路来解决: DFS Union-Find 以下是使用上一篇的数据结构Union-Find来处理的代码: /** * LeetCode_146 * https://leetcode.com/problems/redundant-connection/description/ * https://www.youtube.com/watch?v=4hJ721ce010&list=LLaIZDn4w2rZnhRNMRMelhfg * */ class Solution { fun fi

[Swift]LeetCode684. 冗余连接 | Redundant Connection

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 v

Leetcode之并查集专题-684. 冗余连接(Redundant Connection)

在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成.附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边. 结果图是一个以边组成的二维数组.每一个边的元素是一对[u, v] ,满足 u < v,表示连接顶点u 和v的无向图的边. 返回一条可以删去的边,使得结果图是一个有着N个节点的树.如果有多个答案,则返回二维数组中最后出现的边.答案边 [u, v] 应满足相同的格式 u < v.

LN : leetcode 486 Predict the Winner

lc 486 Predict the Winner 486 Predict the Winner Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a

LN : leetcode 416 Partition Equal Subset Sum

lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of th

LN : leetcode 730 Count Different Palindromic Subsequences

lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7. A subsequence of a string S i

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482