685. Redundant Connection II Hard

@problem@discussion
#Depth-First Search#Breadth-First Search#Union Find#Graph



1/**
2 * [685] 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/uploads/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/uploads/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 */
29pub 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
36impl Solution {
37    pub fn find_redundant_directed_connection(edges: Vec<Vec<i32>>) -> Vec<i32> {
38        vec![]
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_685() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.