Xayahh-Rakann at Moloco (Hard)
Time limit2sMemory limit512 MB
Given n jars with m inseparable pairs, decide whether exactly k jars can be placed in one building so that no inseparable pair is split across the two buildings.
- Level
Medium5 of 10
- Topics
- Graph, Union-find, Math, Combinatorics
- Solved
- No attempts yet
Problem
Moloco is an IT company, but a few of its employees ran forbidden experiments on two kinds of unstable dark matter. They named the pair Xayahh-Rakann after an old legend about a couple who could never be apart.
The employees produced units of unstable dark matter, and each unit sits in its own jar. The jars are labeled .
After many experiments they learned that some pairs of Xayahh-Rakann must not be kept far apart. A pair that is separated becomes unstable, explodes, and wipes out the whole planet. Such a pair of jars is called an inseparable pair, and the employees found of them. Every other pair of jars is safe at any distance.
The building that stores the jars right now is scheduled for renovation. The employees decided to leave exactly jars in the basement, which the renovation does not touch, and to move the other jars to a new office far away. The catch is that an inseparable pair placed in two different buildings explodes and takes the planet with it.
Decide whether the jars can be split into a group of and a group of safely.
Input
The first line contains three integers , , and (, , ). Note that can be larger than .
Each of the next lines contains two integers that describe one inseparable pair of jars. The two integers on a line are different and lie between and . Line describes the -th inseparable pair. The same pair can appear more than once, with the two numbers in either order.
Output
Print SAFE on the first line if the jars can be split safely into jars and jars. Otherwise print DOOMED.