Genetic Reconstruction

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

요약
주어진 부모 정보와 눈 색깔이 두 대립유전자 유전 방식과 모순되지 않는지 판정하고, 가능하면 사전순으로 가장 앞서는 대립유전자 쌍을 출력합니다.
난이도

보통10점 중 7점

유형
백트래킹, 그래프, 구현
정답자
아직 제출이 없습니다

문제

You’re in charge of studying a new species, and you’d like to make sure the work that you’ve collected is correct.

There are several creatures. For each one, you know the eye color they have. This is denoted by one of the first 2020 English lowercase letters (from ‘a’ to ‘t’).

You think that there is a gene that controls eye color. Your hypothesis is that each creature has two Alleles. An allele is represented by a lowercase English letter. The resulting eye color of the creature is the allele of the two which comes first alphabetically.

In addition, you’ve identified two parents of each creature. Some parents’ information may be missing; either both parents’ information will be available or both will be missing. A creature inherits one allele from each parent; any of the four combinations is possible. For example, one parent with alleles ‘ak’ (thus eye color ‘a’), and another with alleles ‘em’ (eye color ‘e’), might have a child with alleles ‘ae’ (eye color ‘a’), ‘am’ (eye color ‘a’), ‘ek’ (eye color ‘e’) or ‘km’ (eye color ‘k’).

Given the number of creatures, the parent information, and the eye color of each creature, determine if this information is consistent with your hypothesis.

입력

The first line of input contains a single integer nn (1≤n≤201≤n≤20), which is the number of creatures in your study. The creatures are numbered from 11 to nn.

Each of the next nn lines contains two integers p_1p\_1, p_2p\_2 ((1≤p_1,p_2≤n1≤p\_1,p\_2≤n, p_1≠p_2p\_1 \ne p\_2) or p_1=p_2=0p\_1=p\_2=0) and a character cc (c∈c \in{‘a’, …\dots, ‘t’}), where p_1p\_1 and p_2p\_2 represent the parents of the given creature (or are both 00 if the parents are unknown), and cc is the given creature’s eye color. Note that either both parents will be known or both will be unknown, and that both parents’ numbers will be less than the number of the given creature; no creature can be its own ancestor or descendant.

출력

If the information is consistent, output the alleles for each creature one per line, with no spaces; otherwise output −1-1. If there are multiple possible solutions, output only the one that comes first alphabetically (e.g., the alphabetically first allele pair for first creature, then second, and so on).

예제2

  1. 예제 1

    입력
    3
    0 0 a
    0 0 b
    1 2 c
    
    예상 출력
    ac
    bc
    cc
    
  2. 예제 2

    입력
    3
    0 0 c
    0 0 c
    2 1 a
    
    예상 출력
    -1