Sandwich Art

시간 제한3초메모리 제한1024 MB

요약
재료 사이의 의존 관계와 장르별 재료 목록이 주어질 때, 재료를 더 넣어 크기 제한 안에서 어떤 장르에 속하는 완성된 샌드위치를 만들 수 있는지 판정한다.
난이도

보통10점 중 5점

유형
그래프, BFS, 해시맵, 그리디
정답자
아직 제출이 없습니다

문제

Zig, the alien, has received one-too-many odd looks for eating a PBPB (Peanut Butter Pickle on Baguette) sandwich. Zig has decided to consult you, a 100% real human being, to help him make his next sandwich and better pretend to be human. Zig has already started making his next sandwich. He would like to know whether the sandwich has potential to be a masterpiece or if it is already a disaster, based on whether or not it follows the rules of sandwich art so far.

There are many different genres of sandwich. For instance, there are peanut butter, deli, egg salad, hamburger, hot dog, breakfast, and grilled cheese sandwiches, to name a few. Some sandwich ingredients are unique to one genre of sandwich (like tuna), but there are some ingredients that can be used by many, not all, sandwich genres (like a hoagie).

Sandwich art has several rules:

  • Certain ingredients may require other ingredients in the sandwich. For example, bagel sandwiches require cream cheese, and jelly requires peanut butter. Note that the reverse is not necessarily true, you can have cream cheese on a sandwich without a bagel.
  • The finished sandwich must not include more than a certain number of ingredients, or else they will be too difficult to eat.

A sandwich is considered finished if it contains all required ingredients. A finished sandwich belongs to a genre if the sandwich contains only (not necessarily all) the ingredients in the genre's ingredient list. A finished sandwich is a masterpiece if it belongs to at least one of the genres.

입력

The first line of input contains 4 integers N G D MN\ G\ D\ M: NN is the total number of ingredients in Zig's sandwich so far; GG is the number of distinct sandwich genres; DD is the number of ingredient dependencies; MM is the maximum number of total ingredients.

The next row contains NN distinct integers, representing the unique IDs of the ingredients already on Zig's sandwich.

The next GG lines each begins with an integer KK, the number of ingredients in the genre, followed by KK distinct integers, representing the unique IDs of the ingredients in the genre.

The next DD lines each contains 2 integers I JI\ J (I≠JI \neq J), denoting that if ingredient II is on the sandwich, then ingredient JJ must also be on the sandwich. All dependencies are distinct.

There may be circular dependencies; for example, a sausage requires a hot dog bun, and a hot dog bun requires a sausage.

All integers are greater than 00 and less than 10610^6, and there are no more than 10610^6 total integers in the input.

출력

If Zig's sandwich is well on its way to becoming a masterpiece, that is, the sandwich is already finished and a masterpiece or has potential to be a masterpiece by adding more ingredients, output masterpiece. Otherwise, output disaster.

예제3

  1. 예제 1

    입력
    2 2 4 3
    1 4
    3 2 4 6
    4 1 2 4 6
    1 2
    3 4
    5 4
    5 6
    
    예상 출력
    masterpiece
    
  2. 예제 2

    입력
    2 2 4 4
    1 3
    3 2 4 6
    4 1 2 4 6
    1 2
    3 4
    5 4
    5 6
    
    예상 출력
    disaster
    
  3. 예제 3

    입력
    2 2 4 2
    1 4
    3 2 4 6
    4 1 2 4 6
    1 2
    3 4
    5 4
    5 6
    
    예상 출력
    disaster