Minimum Union of Two Routes
Problem statement
You are given an undirected, unweighted graph with n vertices numbered from 0 to n - 1. Alice starts at vertex a, Bob starts at vertex b, and both must reach destination d.
Alice chooses one path from a to d, and Bob chooses one path from b to d. The cost of their two routes is the number of distinct undirected edges that appear in the union of both paths.
Return the minimum possible cost. Return -1 when either traveler cannot reach d.
Examples
Example 1
n = 7edges = [[0,2],[1,2],[2,3],[3,6],[0,4],[1,4],[4,5],[5,6]]a = 0b = 1d = 6return = 4The travelers can meet at vertex 2. The edges from 0 and 1 to 2 contribute two edges, and the shared suffix 2 - 3 - 6 contributes two more.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.