leetcode685. Redundant connection II/and check set

Subject: 685. Redundant Connection II

In this problem, a rooted tree refers to a directed graph that meets the following conditions. The tree has only one root node, and all other nodes are successors of this root node. Each node has only one parent node, except for the root node, there is no parent node.

Enter a directed graph consisting of a tree with N nodes (the node values ​​do not repeat 1, 2, …, N) and an additional edge. The two vertices of the additional edge are contained between 1 and N, and this additional edge does not belong to an existing edge in the tree.

The resulting graph is a two-dimensional array of edges. The element of each edge is a pair of [u, v], used to represent the edge connecting vertex u and vertex v in a directed graph, where u is a parent node of v.

Return an edge that can be deleted, so that the remaining graph is a rooted tree with N nodes. If there are multiple answers, return the last answer that appears in the given two-dimensional array.

Example 1:

输入: [[1,2], [1,3], [2,3]]
输出: [2,3]
解释: 给定的有向图如下:
  1
 / \
v   v
2-->3

Example 2:

输入: [[1,2], [2,3], [3,4], [4,1], [1,5]]
输出: [4,1]
解释: 给定的有向图如下:
5 <- 1 -> 2
     ^    |
     |    v
     4 <- 3

note:

  • The size of the two-dimensional array is in the range of 3 to 1000.
  • Each integer in the two-dimensional array is between 1 and N, where N is the size of the two-dimensional array.

Source: LeetCode
Link: https://leetcode-cn.com/problems/redundant-connection-ii The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Basic idea: consolidate and collect

Reference link: https://leetcode-cn.com/problems/redundant-connection-ii/solution/685-rong-yu-lian-jie-iibing-cha-ji-de-ying-yong-xi/
doing this question , First of all want to understand, under what circumstances will additional edges appear

  • When there is a ring in the graph
  • When there are nodes with in-degree 2 in the graph

Use the union search set to determine whether there is a ring in the graph: when the edge has not been added to the join search set, it already has the same root, then the edge is the edge that leads to a ring in the graph.

class Union{
    
    
public:
    vector<int> parent;
    vector<int> level;
    Union(){
    
    
        parent = vector<int>(1010, 0);
        level = vector<int>(1010, 0);
    }
    int find(int x){
    
    //查找当前节点的根节点
        int p = x;
        while(parent[p] != 0){
    
    
            p = parent[p];
        }
        return p;
    }
    
    void merge(int x, int y){
    
    //归并两个节点
        int px = find(x);
        int py = find(y);
        if(level[px] > level[py]){
    
    
            parent[py] = px;
        }
        else if(level[px] < level[py]){
    
    
            parent[px] = py;
        }
        else{
    
    
            parent[px] = py;
            level[py]++;
        }
    }
    
    bool same(int x, int y){
    
    //判断两个节点是否有相同的父亲
        int px = find(x);
        int py = find(y);
        return px == py;
    }
};
class Solution {
    
    
public:
    //判断能否构成一棵树
    bool isTree(vector<vector<int>>& edges, int k){
    
    
        Union U;
        for(int i = 0; i < edges.size(); ++i){
    
    
            if(i == k)
                continue;
            if(U.same(edges[i][0], edges[i][1])){
    
    
                return false;
            }
            U.merge(edges[i][0], edges[i][1]);
        }
        return true;
    }
    
    //确定删除环中的哪一条边
    vector<int> getEdges(vector<vector<int>>& edges){
    
    
        Union U;
        for(int i = 0; i < edges.size(); ++i){
    
    
            if(U.same(edges[i][0], edges[i][1])){
    
    
                return edges[i];
            }
            U.merge(edges[i][0], edges[i][1]);
        }
        return {
    
    };
    }
    
    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
    
    
        //本题利用并查集的思想
        //分两种情况进行考虑
        //图中有环
        //图中没有环,但是有入度为2的点
        
        
        //统计点的入度
        int n = edges.size();
        vector<int> indegree(n + 1, 0);
        
        for(auto e : edges){
    
    
            indegree[e[1]]++;
        }
        
        vector<int> vec;
        for(int i = n - 1; i >= 0; --i){
    
    
            if(indegree[edges[i][1]] == 2){
    
    
                vec.push_back(i);
            }
        }
        
        //如果有度为2的点,判断删除后是否会构成树
        if(vec.size() > 0){
    
    
            if(isTree(edges, vec[0])){
    
    
                return edges[vec[0]];
            }
            else{
    
    
                return edges[vec[1]];
            }
        }
        
        return getEdges(edges);
    }
};

Guess you like

Origin blog.csdn.net/qq_31672701/article/details/108651921