Under construction

Still being built. Things may change

Xayahh-Rakann at Moloco (Hard)

Time limit2sMemory limit512 MB

Summary
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 nn units of unstable dark matter, and each unit sits in its own jar. The jars are labeled 1,2,…,n1, 2, \dots, n.

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 mm 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 kk jars in the basement, which the renovation does not touch, and to move the other n−kn-k 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 kk and a group of n−kn-k safely.

Input

The first line contains three integers nn, mm, and kk (1≤n≤10001 \le n \le 1000, 1≤k≤10001 \le k \le 1000, 1≤m≤10000001 \le m \le 1000000). Note that kk can be larger than nn.

Each of the next mm lines contains two integers that describe one inseparable pair of jars. The two integers on a line are different and lie between 11 and nn. Line i+1i+1 describes the ii-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 kk jars and n−kn-k jars. Otherwise print DOOMED.

Examples5

  1. Example 1

    Input
    3 2 1
    1 2
    2 3
    
    Expected output
    DOOMED
    
  2. Example 2

    Input
    4 4 2
    1 2
    1 2
    3 4
    4 3
    
    Expected output
    SAFE
    
  3. Example 3

    Input
    2 1 1
    1 2
    
    Expected output
    DOOMED
    
  4. Example 4

    Input
    2 1 2
    2 1
    
    Expected output
    SAFE
    
  5. Example 5

    Input
    5 3 3
    1 2
    3 4
    1 2
    
    Expected output
    SAFE