Bacon Number
시간 제한1초메모리 제한1024 MB
각 영화에 출연한 배우 목록이 주어질 때, 두 배우를 연결하는 배우와 영화의 교대 경로를 찾아 출력하거나 경로가 없으면 -1을 출력한다.
문제
Carlinhos loves movies, and recently he has been fascinated by the Bacon Number, which is defined as follows.
- The Bacon number of the actor Kevin Bacon is equal to ;
- If the smallest Bacon number of an actor with whom has appeared in the same movie is , the bacon number of the actor is .
That is, the Bacon number measures the shortest path between any actor and the actor Kevin Bacon, in which two actors are connected if they appeared together in the same movie.
Carlinhos is interested in a more general problem: given two actors, how to connect them through intermediate movies and actors? Given movies, and, for each movie, which of the existing actors acted in it. Carlinhos wants to answer queries: in the -th of them, we want to compute some way to connect actor with actor . We must find some sequence , where are actors and are movies, and actor acted in movies and , or indicate that no such sequence exists.
입력
In the first line of the input, two integers () and () are given, the number of movies and the number of actors.
lines follow. In the -th line, the first integer () denotes the number of actors in movie . Next, numbers in ascending order separated by spaces: the indices, from to , of the actors who acted in movie .
The next line, contains an integer (): the number of queries.
The next lines describe the queries. In the -th of them, read two numbers , (), the actors we want to connect. It is guaranteed that the total number of actors in the movies is at most . That is, .
출력
For each of the queries, if there is no sequence, print a line with -1. Otherwise, print two lines. In the first line, print the number of actors () in some way to connect and . In the second, print the sequence as described, with actors and movies, alternating. If there is more than one way to connect the actors, print any of them.