[LeetCode]685. Redundant Connection II

来源:互联网 发布:aes ecb加密java实现 编辑:程序博客网 时间:2024/06/05 19:15
  • descrption
    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 one parent, except for the root node which has no parents.
    The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, …, N), with one additional directed 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] that represents a directed edge connecting nodes u and v, where u is a parent of child v.
    Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.
    Example 1:
Input: [[1,2], [1,3], [2,3]]Output: [2,3]Explanation: The given directed graph will be like this:  1 / \v   v2-->3

Example 2:

Input: [[1,2], [2,3], [3,4], [4,1], [1,5]]Output: [4,1]Explanation: The given directed graph will be like this:5 <- 1 -> 2     ^    |     |    v     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.
- 解题思路
这道题与上一篇文章的题目类似,但是难度有所提升,因为这道题解决的是有向图的问题。所以就不能简单的根据一条边的两个节点有共同的根节点而确定要删除的边是这条。
现在存在三种情况:
(1)有向图中只有环。这种情况就简单将两个节点具有共同根节点的边删去就好。
(2)有向图中没有环,但有个节点有两个父节点。这种情况就将第二次出现不同父节点的边删去就好。
(3)有向图中既有环,而且有个节点还有两个父节点。这时就检测当除去第二次出现父节点的边后,剩余边是不是合法的,如果不合法证明应该删掉的是另一个父节点的边。

  • 代码如下
class Solution {public:    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {        //root of every node, if the root is the same, then the edge is redundant        int size = edges.size()+1;        vector<int> parent(size);        //ans        vector<int> ans;        //in the begining, every node's root is itself        for(int i = 1; i <= edges.size(); i++){            parent[i] = i;        }        vector<int> e1, e2;        //find out if there is a node has two parent        for(auto e : edges){            if(parent[e[1]] != e[1]) { //have another parent                e1 = {parent[e[1]], e[1]};                e2 = e;            }            else {                parent[e[1]] = e[0];            }        }        for(int i = 1; i <= edges.size(); i++){            parent[i] = i;        }        if(e1.empty()){            for(auto e : edges) {                int u = e[0];                int v = e[1];                parent[u] = findparent(parent, u);                if (parent[u] != parent[v]){                    parent[v] = parent[u];                }                else {                    return e;                }            }        }        else {            for (auto e:edges) {                if(e[0] == e2[0] && e[1] == e2[1])                    continue;                int u = e[0];                int v = e[1];                parent[u] = findparent(parent, u);                if(parent[u] != parent[v]){                    parent[v] = parent[u];                }                else                    return e1;            }        }        return e2;    }    int findparent(vector<int>& parent, int u){        if(parent[u] == u)            return u;        else            return findparent(parent, parent[u]);    }};

原题地址

如有错误请指出,谢谢!