Skip to content
DeepForgeby svx
All problems
MediumGraph Algorithmsgraph-352

Bridges After Extra Tree Edge

Problem

Given a tree plus one extra edge, find which tree edges are still bridges.

tree_edges has n - 1 undirected [u, v] pairs forming a tree, and extra_edge adds exactly one edge, creating one cycle. Every tree edge on the unique cycle stops being a bridge, while all other tree edges remain bridges. Return the surviving bridges as sorted [u, v] pairs with u < v.

Hint

Find the tree path between the extra edge endpoints; exactly those tree edges lie on the new cycle.

Test cases

case 1
in: [4,[[0,1],[1,2],[2,3]],[0,3]]
expected: []
case 2
in: [4,[[0,1],[1,2],[2,3]],[0,1]]
expected: [[1,2],[2,3]]
case 3
in: [5,[[0,1],[0,2],[0,3],[0,4]],[1,2]]
expected: [[0,3],[0,4]]
case 4
in: [2,[[1,0]],[0,1]]
expected: []
case 5
in: [3,[[0,2],[2,1]],[2,1]]
expected: [[0,2]]
Starter codePython
def bridges_after_extra_edge(n, tree_edges, extra_edge):
    # Your code here
    pass

5 test cases · run your code against them in the editor