[LeetCode] (Hard) 685. Redundant Connection II

https://leetcode.com/problems/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 uand 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.

根据题干描述,一共有两种违例的可能:1、一个节点被多个节点指向  2、成环 

其中第一个可能是相比于684的无向图新增的,这就使得这道题不仅仅是消除弱联通分量这么简单。

每一次发现结点违例后删除边的优先级也发生了变化:最优先是违反两个条件的边,其次是两个违例中后面那条出现的边

因为最优先情况的存在我们需要继续向后扫描

class Solution {
public:
    vector root;
    vector findRedundantDirectedConnection(vector>& edges) {
        root = vector(edges.size()+1);
        for(int i = 0; i < root.size(); ++i){
            root[i] = i;
        }
        
        vector candidate;
        vector res;
        
        for(int i = 0; i < edges.size(); ++i){
            int st = edges[i][0];
            int ed = edges[i][1];
            int stR = getRoot(st);
            int edR = getRoot(ed);
            
            if(edR != ed){
                if(!candidate.empty()){
                    res = {root[ed], ed};
                    break;
                }else{
                    candidate = {root[ed], ed};
                    res = {st, ed};
                }
            }else if(stR == edR){
                if(!candidate.empty()){
                    res = candidate;
                    break;
                }else{
                    candidate = res = {st, ed};
                }
            }else{
                root[ed] = st;
            }
        }
        
        return res;
        
        
    }
    
    int getRoot(int idx){
        while(root[idx] != idx) idx = root[idx];
        return idx;
    }
};

其中candidate用于标记是否已发现至少一种违例,在第一种违例下还用于记录当前参与讨论的边。

主逻辑如下:

1. 如果发生了第一种违例

1.1. 如果已经发生了第二种违例,则删除此前的父亲边(这条边必然参与环的组成)

1.2. 如果没有发生第二种违例,则记录当前违例的两条边,当前边优先级更高,当前边不计入图中,前边继续参与环的检测

2. 如果发生第二种违例

2.1. 如果已经发生了第一种违例,则删除第一种为违例中参与构造环的那条边

2.2. 如果没有发生第一种违例,则标记第二种违例的发生,并且按照优先级应当记录当前边(比环中其他边的优先级要高)

你可能感兴趣的:(LeetCode,图,LeetCode)