Illumination
Time limit2sMemory limit512 MB
Given lamps on an n by n grid, decide whether each lamp can point along its row or column so that no cell gets same-direction light from two lamps.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, Math, Implementation
- Solved
- No attempts yet
Problem
You inherited a haunted house. Its floor plan is an square grid with no interior walls, and lamps sit at fixed cells.
Each lamp lights either the row it stands in or the column it stands in, never both at once. The light reaches cells in both directions along the chosen line, so a lamp that no exterior wall blocks lights as many as cells, counting its own.
If one cell is lit by two lamps that both light a row, or by two lamps that both light a column, the bright spot drives the ghosts away for good and your property loses value. A cell lit by one lamp that lights a row together with one lamp that lights a column causes no trouble.
Decide whether every lamp can be given a row or a column without driving the ghosts away.
Input
The first line contains three positive integers , and ().
Each of the next lines contains two positive integers and (), meaning that a lamp stands in row and column .
All lamp positions are distinct.
Output
Print 1 on a single line if such an assignment exists, and 0 otherwise.