Leetcode算法学习日志-684 Redundant Connection

来源:互联网 发布:web编程语言有哪些 编辑:程序博客网 时间:2024/06/06 22:54

Leetcode 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] withu < v, that represents an undirected edge connecting nodesu 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(并查集)

    深度优先搜索

    该方法沿着树的一条路往下走,先保存第一个连接对的两个元素于set中,并将该对从集合中删除,重新遍历连接对集合,如果下一个连接对两个元素都在set中,说明走出了一个回路,返回该对即为结果;如果有一个元素在set中,则可以延这条路继续走,并将该对删除;如果两个都不在set中,则可以暂时跳过。C++代码如下:

    class Solution {public:    vector<int> findRedundantConnection(vector<vector<int>>& edges) {        unordered_set<int> attened;        vector<int> res;        if(edges.size()<3)            return res;        attened.insert(edges[0][0]);        attened.insert(edges[0][1]);        for(int i=1;i<edges.size();i++){            if(attened.count(edges[i][0])&&attened.count(edges[i][1]))                return edges[i];            else if((!attened.count(edges[i][0]))&&(!attened.count(edges[i][1])))                continue;              else{                if(attened.count(edges[i][0]))                    attened.insert(edges[i][1]);                else                    attened.insert(edges[i][0]);                  edges.erase(edges.begin()+i);                i=0;            }          }        }};

    Union Find

    并查集可以根据已经Union后形成的连接情况判断两个点是否连接,同时也可以在Union过程中判断两个点是否连接,以避免重复添加连接关系使他们连接成圈。本题最后添加的那个使得树产生圈的边就可以通过边union边检测的方式检查出来。C++代码如下:

    class Solution {public:    int find(int a,int *P){        int res=a;        while(res!=P[res])            res=P[res];        return res;       }    vector<int> findRedundantConnection(vector<vector<int>>& edges) {        int *parent=new int[edges.size()+1];        for(int i=1;i<=edges.size();i++){            parent[i]=i;          }          for(auto e:edges){            if(find(e[0],parent)==find(e[1],parent))                return e;            parent[find(e[1],parent)]=find(e[0],parent);                }      }};
    union过程中是将find得到的根节点间确定新的父子关系。



    阅读全文
    0 0
    原创粉丝点击