#LCA0000050. Rạp xiếc của những chú bọ chét (Flea Circus)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Rạp xiếc của những chú bọ chét (Flea Circus)
Flea Circus
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
Two fleas start at vertices and of an undirected tree. They simultaneously jump toward each other along their unique shortest path, traversing one edge per jump.
If the path contains an even number of edges, they meet at one vertex. Otherwise, they eventually keep jumping back and forth between two adjacent vertices. For each starting pair, report the meeting vertex or the two alternating vertices, listed in ascending order.
Input
- The first line contains .
- The next lines contain edges .
- The next line contains .
- The following lines contain starting positions .
The original problem ends a sequence of datasets with ; the PHOJ interface uses the single dataset described above.
Output
For each query print exactly one of:
The fleas meet at x.if the fleas meet at vertex .The fleas jump forever between x and y.if they alternate between .
Preserve capitalization, spaces, and the terminal period.
Subtasks
General constraints: ; ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
8
1 2
1 3
2 4
2 5
3 6
3 7
5 8
5
5 1
7 4
1 8
4 7
7 8
Output
The fleas meet at 2.
The fleas meet at 1.
The fleas jump forever between 2 and 5.
The fleas meet at 1.
The fleas jump forever between 1 and 2.
Explanation
For , the path has two edges so the fleas meet at . The path yields a meeting at . The path has two central adjacent vertices . Query meets at ; on , the two middle vertices are .