Lights
Time limit1sMemory limit128 MB
Each switch toggles a light and its neighbors; find the minimum number of switches to press so that all N lights end up on.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Math, Brute force, Graph
- Solved
- No attempts yet
Problem
Bessie and the cows were playing games in the barn, but the power was reset and all the lights turned off. Help the cows turn every light back on so they can resume their games.
There are lights (), numbered . The lights and their switches are wired into a complex network with () connections, each joining a pair of lights.
Every light has a switch. Toggling a switch flips that light and all of the lights directly connected to it — each of them changes state (on becomes off, off becomes on).
Find the minimum number of switches you must toggle to turn all the lights back on.
It is guaranteed that at least one way to toggle the switches turns every light on.
Input
- Line : Two space-separated integers, and .
- Lines : Each line contains two space-separated integers, the two lights joined by a connection. No pair is repeated.
Output
Print a single integer: the minimum number of switches that must be toggled to turn on all the lights.
Hint
There are 5 lights. Lights 1, 4, and 5 are each connected to both light 2 and light 3.
Toggle the switches on lights 1, 4, and 5.