Loop in the Wiring
A circuit has junctions numbered 0 to len(neighbors) - 1. neighbors[i] lists the junctions that share a wire with junction i. Wires are undirected and the lists are symmetric: if j is in neighbors[i] then i is in neighbors[j]. No junction is wired to itself and no wire is listed twice.
Return true if the wiring contains a loop, meaning a route that leaves a junction along some wire and comes back to it without crossing any wire twice. Otherwise return false. A circuit with no wires has no loop.
Example 1
neighbors [[1,2],[0,2],[0,1]]
returns true
Junctions 0, 1 and 2 are wired in a triangle.
Example 2
neighbors [[1],[0,2],[1]]
returns false
The three junctions form a single line, which has no loop.
Constraints
1 <= len(neighbors) <= 500 <= neighbors[i][j] < len(neighbors)- At most 50 wires, so at most 100 entries in total
- The lists are symmetric, with no self wires and no repeated wire
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Mutual FollowsEasy
- Paint BucketEasy
- Pointer Loop LengthEasy
- Rumour DaysEasy
- Same Network QueriesEasy
- Tunnels to Every StationEasy
- Blocks to the Nearest ShopMedium
- Build Order ExistsMedium