Under construction

Still being built. Things may change

Door Man

Time limit1sMemory limit128 MB

Summary
You decide whether one walk from the start room closes every open door exactly once and ends in room 0.
Level

Medium4 of 10

Topics
Graph, DFS
Solved
No attempts yet

Problem

You are a butler in a large mansion. The mansion has so many rooms that they go by number alone (room 0, room 1, room 2, room 3, and so on). Your master is an absent-minded lout who wanders one floor of the house and leaves every door he passes standing open. Over the years you have learned to walk a single path through the messy rooms and pull each door shut behind you.

The hard part is deciding whether a path exists that obeys all three rules:

  1. Always shut an open door behind you right after you pass through it.
  2. Never open a closed door.
  3. Finish in your chambers, room 0, with every door closed.

You are given the list of rooms, the doors that stand open between them, and the room you start in. You do not have to produce a route, only decide whether one exists.

Input

The input holds at least 1 and at most 100 data sets. Each data set follows the format below, and no blank line separates one data set from the next.

A data set has three parts.

  1. Start line: a single line "START M N", where M is the butler's starting room and N is the number of rooms in the house (1≤N≤201 \le N \le 20).
  2. Room list: N lines. Each line lists, for one room, every open door that leads to a room of higher number. If room 3 has open doors to rooms 1, 5, and 7, the line for room 3 reads "5 7". The first line of the list is room 0, the second is room 1, and the last is room N−1N-1. A line may be empty. The last line is always empty, since it belongs to the highest numbered room. Within a line the adjacent rooms always appear in ascending order. Two rooms may be joined by several doors.
  3. End line: a single line "END".

After the final data set comes a single line "ENDOFINPUT".

No single data set contains more than 100 doors.

Output

Print exactly one line per data set. If the butler can follow the rules above, walk into his chambers, and close the last open door behind him, print "YES X", where X is the number of doors he closed. Otherwise print "NO".

If no door is open and the butler already stands in room 0, he satisfies every rule after closing 0 doors, so print "YES 0".

Examples1

  1. Example 1

    Input
    START 1 2
    1
    
    END
    START 0 5
    1 2 2 3 3 4 4
    
    
    
    
    END
    START 0 10
    1 9
    2
    3
    4
    5
    6
    7
    8
    9
    
    END
    ENDOFINPUT
    
    Expected output
    YES 1
    NO
    YES 10