ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

LeetCode 685. Redundant Connection II 冗余连接 II (C++/Java)

2020-02-25 15:04:31  阅读:309  来源: 互联网

标签:node Java int edges Redundant II edge root roots


题目:

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

分析:

这道题是LeetCode 684. Redundant Connection 冗余连接(C++/Java)的进阶版,还是利用并查集的思路做,不会的同学可以先看一下前面的题解。

685题将图更改为有向图,最后要求删去一条边得到一颗树,也就是要求只能有一个根节点,每一个节点只有一个父节点。

那么如果在图中有一个节点有两个父结点时,则要删去的边就必定是这两条边中构成环的一条,如果所有的节点都只有一个父结点,那么删去的边就是构成环的那一条边,基于这个思路,先遍历所有的边,存储他们的父亲节点,当发现有节点的父节点已存在时,将原来的父结点和这个节点记做结果1,新的父节点和这个节点记做结果2,在这里我们将后出现的边做一个标记。因为在做并查集时,发现有环的话,无法立刻判断出这两条边到底那条在环中,所以我们将新来的边做标记,在构建并查集时,不将新的边加入,那么如果最后图内无环则直接返回结果2,如果有环且结果1不为空就返回结果1,如果结果1为空则表明不存在多个父结点的节点,那么就和684题一样了,直接返回构成环的边即可。

程序:

C++

class Solution {
public:
    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
        int n = edges.size();
        vector<int> parents(n+1, 0);
        vector<int> root(n+1, 0);
        vector<int> res1;
        vector<int> res2;
        for(auto &edge:edges){
            int u = edge[0];
            int v = edge[1];
            if(parents[v] > 0){
                res1 = {parents[v], v};
                res2 = edge;
                edge[0] = -1;
                edge[1] = -1;
            }
            parents[v] = u;
        }
        for(auto edge:edges){
            int u = edge[0];
            int v = edge[1];
            if(u < 0 || v < 0)
                continue;
            if(!root[u]) root[u] = u;
            if(!root[v]) root[v] = v;
            int pu = find(u, root);
            int pv = find(v, root);
            //有环
            if(pu == pv){
                return res1.empty() ? edge : res1;
            }
            root[pv] = pu;
        }
        return res2;
    }
private:
    int find(int node, vector<int>& root){
        while(root[node] != node){
            root[node] = root[root[node]];
            node = root[node];
        }
        return node;
    }
};

Java

class Solution {
    public int[] findRedundantDirectedConnection(int[][] edges) {
        int n = edges.length;
        int[] parents = new int[n+1];
        int[] roots = new int[n+1];
        int[] res1 = null;
        int[] res2 = null;
        for(int[] edge:edges){
            int u = edge[0];
            int v = edge[1];
            if(parents[v] > 0){
                res1 = new int[]{parents[v], v};
                res2 = new int[]{u, v};
                edge[0] = -1;
                edge[1] = -1;
            }
            parents[v] = u;
        }
        for(int[] edge:edges){
            int u = edge[0];
            int v = edge[1];
            if(u < 0 || v < 0)
                continue;
            if(roots[u] == 0) roots[u] = u;
            if(roots[v] == 0) roots[v] = v;
            int pu = find(u, roots);
            int pv = find(v, roots);
            if(pu == pv){
                return res1 == null ? edge : res1;
            }
            roots[pv] = pu;
        }
        return res2;
    }
    private int find(int node, int[] roots){
        while(node != roots[node]){
            roots[node] = roots[roots[node]];
            node = roots[node];
        }
        return node;
    }
}

 

标签:node,Java,int,edges,Redundant,II,edge,root,roots
来源: https://www.cnblogs.com/silentteller/p/12361741.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有