Telepathy

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

요약
같은 나무를 서로 다른 이름으로 표시한 지도를 가진 두 사람이 대화 없이 각자 이동 경로를 정해 6d턴 안에 같은 지점에서 만나야 한다.
난이도

어려움10점 중 9점

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

문제

Aitana and Bruno are visiting a national park in Bolivia. In the national park, there are NN sites, and there are N−1N −1 roads connecting two sites. It is possible to move from any site to any site by passing through some roads.

When they were walking in the national park, they got separated from each other. From now on they must meet again by reaching the same site at the same time. However, being deep in the Amazon rainforest, they cannot communicate with each other. The only thing they can rely on is their own map, which depicts the road structure of the national park. Each of them wrote labels 0,1,…,N−10, 1, \dots , N − 1 for each site in his/her map. However, the labeling of Aitana and Bruno may be different.

Aitana and Bruno now start moving to meet again. For each turn, they simultaneously perform either of the following actions: move to the site that is directly connected by road to the current site, or stay at the current site.

Write a program that implements a strategy to make Aitana and Bruno meet again. In this problem, a submission receives the full score if they meet again within 6d6d turns, where dd is the minimum number of roads to pass to move from Aitana’s current site to Bruno’s current site. Note that, when they come to the same place in the middle of the road, it is not considered that they meet again.

In this problem, one must solve for QQ scenarios in a single run of the program.

Problem Details

In this section, we formally explain the problem. Each site of the national park is assigned an ID from 00 to N−1N − 1, and the jj-th road (0≤j≤N−20 ≤ j ≤ N − 2) connects the site with ID u_ju\_j and ID v_jv\_j. For the site with ID ii (0≤i≤N−10 ≤ i ≤ N − 1), label p_ip\_i is written on Aitana’s map, and label q_iq\_i is written on Bruno’s map. Here, (p_0,p_1,…,p_N−1)(p\_0, p\_1, \dots , p\_{N−1}) and (q_0,q_1,…,q_N−1)(q\_0, q\_1, \dots , q\_{N−1}) are permutations of (0,1,…,N−1)(0, 1, \dots , N − 1).

Aitana knows that, for each j=0,1,…,N−2j = 0, 1, \dots , N − 2, there is a road connecting the sites labeled A_jA\_j and B_jB\_j, and Aitana is currently at the site labeled SS. The “label” here is based on the Aitana’s map. Hence, the jj-th road (0≤j≤N−20 ≤ j ≤ N − 2) connects the sites labeled p_u_jp\_{u\_j} and label p_v_jp\_{v\_j} , and S=p_sS = p\_s holds where ss is the ID of Aitana’s current site. However, the roads may not be given in the order, and the two sites that each road connects may not be given in the order of u_ju\_j, v_jv\_j. Similarly, Bruno knows that, for each j=0,1,…,N−2j = 0, 1, \dots , N − 2, there is a road connecting the sites labeled C_jC\_j and D_jD\_j, and Bruno is currently at the site labeled TT. The “label” here is based on the Bruno’s map. Especially, T=q_tT = q\_t holds where tt is the ID of Bruno’s current site.

Based on the information above, Aitana and Bruno decide their movement of the next 10N10N turns. In other words, Aitana decides the sequence of labels x_0,x_1,…,x_10Nx\_0, x\_1, \dots , x\_{10N}, and Bruno decides the sequence of labels y_0,y_1,…,y_10Ny\_0, y\_1, \dots , y\_{10N}, which represents his/her movement, independently. They must satisfy the following conditions:

  • x_0=Sx\_0 = S, and for each kk (1≤k≤10N1 ≤ k ≤ 10N), the sites labeled x_k−1x\_{k−1} and x_kx\_k in Aitana’s map are either the same site or directly connected by a road.
  • y_0=Ty\_0 = T, and for each kk (1≤k≤10N1 ≤ k ≤ 10N), the sites labeled y_k−1y\_{k−1} and y_ky\_k in Bruno’s map are either the same site or directly connected by a road.

The turn number k∗k^∗ when Aitana and Bruno meet again is the minimum kk such that label x_kx\_k (in Aitana’s map) and label y_ky\_k (in Bruno’s map) represent the same site. A submission receives the full score if k∗≤6dk^∗ ≤ 6d.

제한

In this problem, you need to solve the problem for at most 201201 scenarios, that is, 1≤Q≤2011 ≤ Q ≤ 201. Each scenario satisfies the following constraints.

  • 2≤N≤2002 ≤ N ≤ 200.
  • (p_0,p_1,…,p_N−1)(p\_0, p\_1, \dots , p\_{N−1}) is the permutation of the integers between 00 and N−1N − 1 (inclusive).
  • (q_0,q_1,…,q_N−1)(q\_0, q\_1, \dots , q\_{N−1}) is the permutation of the integers between 00 and N−1N − 1 (inclusive).
  • 0≤u_j≤N−10 ≤ u\_j ≤ N − 1 (0≤j≤N−20 ≤ j ≤ N − 2).
  • 0≤v_j≤N−10 ≤ v\_j ≤ N − 1 (0≤j≤N−20 ≤ j ≤ N − 2).
  • It is possible to move from any site to any site by passing through some roads.
  • 0≤s≤N−10 ≤ s ≤ N − 1.
  • 0≤t≤N−10 ≤ t ≤ N − 1.
  • s≠ts \ne t.

예제

이 문제는 공개된 예제가 없습니다.