Wikipedia Black Hole

면접 대비

시간 제한1초메모리 제한2048 MB

요약
위키백과 문서 링크를 방향 그래프로 주고 시작 문서에서 출발해 다시 시작 문서로 돌아오는 최단 사이클의 길이를 구한다. 없으면 NO BLACK HOLE을 출력한다.
난이도

보통10점 중 4점

유형
그래프, BFS, 최단 경로, 해시맵
정답자
아직 제출이 없습니다

문제

You have found yourself on Wikipedia reading the article about black holes. You don't understand everything on the page, but there are links to other articles to explain the unfamiliar concepts. There are so many links to choose from, but you decide to click on the link to the article about "general relativity" (such light reading). You read a little bit about it, but realize that you'll need to learn about "partial differential equations" before you can understand any of the mathematics behind general relativity, so you click on that article.

As you keep clicking, you wonder if it's possible for you to eventually end up back on the original article about black holes. It would be bad if that was the case because it would mean you could keep clicking through the same articles forever. To avoid this fate, you decide to write a program that will tell you if it's possible to end up back on the article you started on, and if so, the fewest number of clicks it would take. Note that there could be loops of links that do not lead back to the original article, but you can ignore those loops.

In the first sample input, the fewest clicks to get back to the original article (listed on the second line) is 33. The path is "black_holes" →\rightarrow "general_relativity" →\rightarrow "albert_einstein" →\rightarrow "black_holes". Note that there is another way to get back to black holes that is 44 clicks ("black_holes" →\rightarrow "escape_velocity" →\rightarrow "speed_of_light" →\rightarrow "gravitational_wave" →\rightarrow "black_holes"), but you only want to know the path with the fewest number of clicks.

입력

The first line of input will be 1≤N≤10,0001 \leq N \leq 10\\,000, the number of links to follow.

The second line of input contains the name of the article you started on. Article names are strings whose length is between 11 and 3030, consisting of only lower-case letters (a-z) and underscores (_).

The next NN lines will each contain the information about a single link. Each of these lines will have two space-separated article names (as defined in the previous paragraph). The first word represents the source article of the link, and the second word represents the destination article. For example, if there is a link on the article about "black_holes" to the article on "general_relativity", then the source would be "black_holes" and the destination would be "general_relativity".

Input Restrictions

  • You are guaranteed the source and destination of each link will be a different article.
  • You are guaranteed that each link is unique. That is, a source article will not contain more than one link to the same destination article.
  • It is possible for an article to not contain any links, and thus not be the source article for any link.
  • Conversely, it is possible for one source article to have links to several destination articles.

출력

If you can get back to the original article by following the links, output the smallest number of links that must be clicked to get back to the original article. If you cannot get back to the original article, output "NO BLACK HOLE".

예제2

  1. 예제 1

    입력
    10
    black_holes
    black_holes general_relativity
    black_holes escape_velocity
    escape_velocity velocity
    escape_velocity speed_of_light
    speed_of_light gravitational_wave
    gravitational_wave black_holes
    general_relativity partial_differential_equations
    general_relativity albert_einstein
    albert_einstein black_holes
    albert_einstein quantum_mechanics
    
    예상 출력
    3
    
  2. 예제 2

    입력
    3
    apples
    apples banana
    banana clementine
    clementine date
    
    예상 출력
    NO BLACK HOLE