Data Structures & AlgorithmsHardCodeforces Div2 D · graph connectivity
Problem: Undirected graph on 5 vertices has edges (0,1), (1,2), (2,0), (2,3), (3,4). Count the bridges by deleting each edge in turn and testing connectivity.
Problem: Undirected graph on 5 vertices has edges (0,1), (1,2), (2,0), (2,3), (3,4). Count the bridges by deleting each edge in turn and testing connectivity.
def connected(n, edges):
p = list(range(n))
def f(x):
while p[x] != x:
x = p[x]
return x
for u, v in edges:
p[f(u)] = f(v)
return len({f(i) for i in range(n)}) == 1
E = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4)]
print(sum(1 for i in range(len(E)) if not connected(5, E[:i] + E[i + 1:])))What is printed?- A. 2
- B. 1
- C. 3
- D. 4
Correct answer: A. 2
Deleting (2,3) or (3,4) disconnects the graph, while the triangle edges are not bridges. A bridge is an edge whose removal increases the number of components.
More Coding Interview Problems questions
- Problem: Topological order by DFS finishing times (reverse postorder) for the DAG adj = {0: [1, 2], 1: [3], 2: [3], 3: []} on 4 vertices…
- Problem: Find the length of the longest common substring (contiguous) of 'abcdef' and 'zcdemn'. python def lcssub(a, b): best = 0 dp =…
- Problem: How many (possibly overlapping) occurrences of the pattern 'ana' are in 'banana'? python s = 'banana' p = 'ana' print(sum(1 for i…
- Problem: Each element of [2, 3, 1, 1, 4] is the maximum jump length from that index. What is the minimum number of jumps to reach the last…
- Problem: A circular route has gas = [1, 2, 3, 4, 5] and cost = [3, 4, 5, 1, 2] (cost to travel from station i to i+1). Find the start…
- Problem: What is the minimum number of intervals to remove so that the rest are pairwise non-overlapping (touching at an endpoint is…