Redundant Connection II

来源:互联网 发布:linux操作系统移植 编辑:程序博客网 时间:2024/05/28 05:14

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 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.

FROM:
LeetCode-Algorithms-graph

这道题要我们从给出边中删去一条边,使有向图成为一棵树。
显然有两种情况:
1.有个节点有两个父亲
2.有环
因此我们先遍历一次所有边,看看是否有节点有两个父亲,有的话记录下来,记这两条边为e1,e2,并且把后面出现的那条边e2删除,然后判断剩余边是否是一棵合法树,是则应删除e2,否则应删除e1。
如果没有节点有两个父亲,就意味着有一个包括了根节点的环。一条一条边加入构图,看看是否成环,只需判断加入的边u,v是否有共同祖先。

class Solution {public:    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {        int parent[1001];        int n, u, v, parent_u;        vector<int> e1, e2;        for (int i = 0; i < 1001; i++) {            parent[i] = -1;        }        for (auto edge:edges) {            if (parent[edge[1]] != -1) {                e1 = {parent[edge[1]],edge[1]};                e2 = edge;            } else {                parent[edge[1]]= edge[0];            }        }        for (int i = 0; i < 1001; i++) {            parent[i] = i;        }        if (e1.empty()) {            for (auto edge:edges) {                u = edge[0];                v = edge[1];                parent[u] = getParent(parent, u);                if (parent[u] != parent[v]) {                    parent[v] = parent[u];                } else {                    return edge;                }            }        } else {            for (auto edge:edges) {                if (edge[1] == e2[1] && edge[0] == e2[0]) continue;                u = edge[0];                v = edge[1];                parent[u] = getParent(parent, u);                if (parent[u] != parent[v]) {                    parent[v] = parent[u];                } else {                    return e1;                }            }        }        return e2;    }    int getParent(int* parent, int i) {        if (parent[i] == i) {            return i;        } else {            return getParent(parent, parent[i]);        }    }};
原创粉丝点击