Tap here to switch tabs
Problem
Submissions

Problem: Redundant Connection II

Try to solve the Redundant Connection II problem.

Statement

A directed graph started as a rooted tree with nn nodes labeled from 11 to nn, having exactly one root node with no incoming edges and every other node having exactly 11 incoming edge, such that all nodes are reachable from the root.

An additional directed edge was added to the tree. The resulting graph is provided as a 22D array edges of length nn, where edges[i] = [u_i, v_i] represents a directed edge pointing from node u_i to node v_i.

Return an edge that can be removed so that the remaining graph is a valid rooted tree of nn nodes. If multiple answers exist, return the edge that appears last in the given edges array.

Constraints:

  • n ==== edges.length

  • 3≤3 \leq n ≤1000\leq 1000

  • edges[i].length ==2== 2

  • 1≤1 \leq u_i, v_i ≤\leq n

  • u_i ≠\neq v_i

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Redundant Connection II

Try to solve the Redundant Connection II problem.

Statement

A directed graph started as a rooted tree with nn nodes labeled from 11 to nn, having exactly one root node with no incoming edges and every other node having exactly 11 incoming edge, such that all nodes are reachable from the root.

An additional directed edge was added to the tree. The resulting graph is provided as a 22D array edges of length nn, where edges[i] = [u_i, v_i] represents a directed edge pointing from node u_i to node v_i.

Return an edge that can be removed so that the remaining graph is a valid rooted tree of nn nodes. If multiple answers exist, return the edge that appears last in the given edges array.

Constraints:

  • n ==== edges.length

  • 3≤3 \leq n ≤1000\leq 1000

  • edges[i].length ==2== 2

  • 1≤1 \leq u_i, v_i ≤\leq n

  • u_i ≠\neq v_i