| 
 | 1 | +/**  | 
 | 2 | + * [0685] Redundant Connection II  | 
 | 3 | + *  | 
 | 4 | + * 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.  | 
 | 5 | + * The given input is a directed graph that started as a rooted tree with n nodes (with distinct values from 1 to 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.  | 
 | 6 | + * The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [ui, vi] that represents a directed edge connecting nodes ui and vi, where ui is a parent of child vi.  | 
 | 7 | + * 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.  | 
 | 8 | + *    | 
 | 9 | + * Example 1:  | 
 | 10 | + * <img alt="" src="https://assets.leetcode.com/uploadisjoint_set/2020/12/20/graph1.jpg" style="width: 222px; height: 222px;" />  | 
 | 11 | + * Input: edges = [[1,2],[1,3],[2,3]]  | 
 | 12 | + * Output: [2,3]  | 
 | 13 | + *  | 
 | 14 | + * Example 2:  | 
 | 15 | + * <img alt="" src="https://assets.leetcode.com/uploadisjoint_set/2020/12/20/graph2.jpg" style="width: 222px; height: 382px;" />  | 
 | 16 | + * Input: edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]  | 
 | 17 | + * Output: [4,1]  | 
 | 18 | + *  | 
 | 19 | + *    | 
 | 20 | + * Constraints:  | 
 | 21 | + *  | 
 | 22 | + * 	n == edges.length  | 
 | 23 | + * 	3 <= n <= 1000  | 
 | 24 | + * 	edges[i].length == 2  | 
 | 25 | + * 	1 <= ui, vi <= n  | 
 | 26 | + * 	ui != vi  | 
 | 27 | + *  | 
 | 28 | + */  | 
 | 29 | +pub struct Solution {}  | 
 | 30 | + | 
 | 31 | +// problem: https://leetcode.com/problems/redundant-connection-ii/  | 
 | 32 | +// discuss: https://leetcode.com/problems/redundant-connection-ii/discuss/?currentPage=1&orderBy=most_votes&query=  | 
 | 33 | + | 
 | 34 | +// submission codes start here  | 
 | 35 | + | 
 | 36 | +impl Solution {  | 
 | 37 | +    // Credit: https://leetcode.com/problems/redundant-connection-ii/discuss/108058/one-pass-disjoint-set-solution-with-explain  | 
 | 38 | +    pub fn find_redundant_directed_connection(edges: Vec<Vec<i32>>) -> Vec<i32> {  | 
 | 39 | +        let n = edges.len();  | 
 | 40 | +        let mut parents = vec![-1; n + 1];  | 
 | 41 | +        let mut disjoint_set = vec![0; n + 1];  | 
 | 42 | +        let mut x1 = -1i32;  | 
 | 43 | +        let mut x2 = -1i32;  | 
 | 44 | +        let mut xn = -1i32;  | 
 | 45 | + | 
 | 46 | +        for i in 0..n {  | 
 | 47 | +            let p = edges[i][0];  | 
 | 48 | +            let c = edges[i][1];  | 
 | 49 | +            if parents[c as usize] != -1 {  | 
 | 50 | +                x1 = parents[c as usize];  | 
 | 51 | +                x2 = i as i32;  | 
 | 52 | +                continue;  | 
 | 53 | +            }  | 
 | 54 | +            parents[c as usize] = i as i32;  | 
 | 55 | +            let p1 = Self::dfs_helper(&mut disjoint_set, p);  | 
 | 56 | +            if p1 == c {  | 
 | 57 | +                xn = i as i32;  | 
 | 58 | +            } else {  | 
 | 59 | +                disjoint_set[c as usize] = p1;  | 
 | 60 | +            }  | 
 | 61 | +        }  | 
 | 62 | + | 
 | 63 | +        if xn == -1 {  | 
 | 64 | +            return edges[x2 as usize].clone();  | 
 | 65 | +        }  | 
 | 66 | + | 
 | 67 | +        if x2 == -1 {  | 
 | 68 | +            return edges[xn as usize].clone();  | 
 | 69 | +        }  | 
 | 70 | + | 
 | 71 | +        edges[x1 as usize].clone()  | 
 | 72 | +    }  | 
 | 73 | + | 
 | 74 | +    fn dfs_helper(disjoint_set: &mut Vec<i32>, i: i32) -> i32 {  | 
 | 75 | +        if disjoint_set[i as usize] == 0 {  | 
 | 76 | +            i  | 
 | 77 | +        } else {  | 
 | 78 | +            disjoint_set[i as usize] = Self::dfs_helper(disjoint_set, disjoint_set[i as usize]);  | 
 | 79 | +            disjoint_set[i as usize]  | 
 | 80 | +        }  | 
 | 81 | +    }  | 
 | 82 | +}  | 
 | 83 | + | 
 | 84 | +// submission codes end  | 
 | 85 | + | 
 | 86 | +#[cfg(test)]  | 
 | 87 | +mod tests {  | 
 | 88 | +    use super::*;  | 
 | 89 | + | 
 | 90 | +    #[test]  | 
 | 91 | +    fn test_0685_example_1() {  | 
 | 92 | +        let edges = vec![vec![1, 2], vec![1, 3], vec![2, 3]];  | 
 | 93 | +        let result = vec![2, 3];  | 
 | 94 | + | 
 | 95 | +        assert_eq!(Solution::find_redundant_directed_connection(edges), result);  | 
 | 96 | +    }  | 
 | 97 | + | 
 | 98 | +    #[test]  | 
 | 99 | +    fn test_0685_example_2() {  | 
 | 100 | +        let edges = vec![vec![1, 2], vec![2, 3], vec![3, 4], vec![4, 1], vec![1, 5]];  | 
 | 101 | +        let result = vec![4, 1];  | 
 | 102 | + | 
 | 103 | +        assert_eq!(Solution::find_redundant_directed_connection(edges), result);  | 
 | 104 | +    }  | 
 | 105 | +}  | 
0 commit comments