Switches and Lamps
InterviewTime limit2sMemory limit512 MB
Given N switches that each light some lamps and together cover every lamp, decide whether leaving out one switch still covers all lamps.
- Level
Easy3 of 10
- Topics
- Implementation, Hash map, Greedy, Array
- Solved
- No attempts yet
Problem
Sangdo has N switches and M lamps. A switch can turn on a lamp. The number of lamps connected to a switch is 0 or more.
Initially, all lamps are off.
Pressing a switch turns on the lamps it is connected to. A switch cannot turn a lamp off. For example, if two switches are connected to one lamp, pressing either switch or pressing both switches leaves the lamp on.
Pressing all N switches turns on every lamp. Sangdo wonders whether pressing N-1 switches can also turn on every lamp.
Given the connections between the switches and the lamps, determine whether pressing N-1 switches can turn on every lamp.
Input
The first line contains the number of switches N and the number of lamps M. The next N lines describe the switches. The first integer on each line is the number of lamps connected to that switch, followed by the indices of the connected lamps separated by spaces.
Switches and lamps are numbered starting from 1.
Only inputs for which pressing all N switches turns on every lamp are given.
Output
Print 1 if pressing N-1 switches can turn on every lamp, and 0 otherwise.
Constraints
- 1 ≤ N, M ≤ 2,000