An electrician wired up n junction boxes, numbered 1 to n, using m wires. Each wire connects two DIFFERENT junction boxes, and no two wires connect the exact same pair of boxes. The wiring may or may not be fully connected.
A loop exists if some junction box can reach itself again by following two or more distinct wires without immediately reusing the wire it just came from. Determine whether the wiring contains at least one loop anywhere.
Line 1: two integers n and m — number of junction boxes (1-indexed) and number of wires.
Next m lines: two integers u v — an undirected wire between junction box u and box v (u ≠ v). No unordered pair {u, v} appears more than once.
A single integer: 1 if the wiring contains a loop, 0 otherwise.
Example 1
Input
4 4 1 2 2 3 3 4 4 1
Expected
1
Explanation
Wires 1-2-3-4-1 form a loop back to junction 1. Output 1.
Example 2
Input
4 3 1 2 2 3 3 4
Expected
0
Explanation
A simple chain with no loop. Output 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →