编程介的小学生 2017-04-09 14:32 采纳率: 20.3%
浏览 809
已采纳

Old Labels

Several thousand years ago, there was an ancient kingdom consisting of n cities numbered from 0 to n - 1. The city numbered 0 was the capital of the kingdom and there were n - 1 directed roads connecting the capital and other cities so that every other city could be reached from the capital.

In the National Library, there was a dusty book in a deep recess which recorded an amazing story. The story said that long long ago, for every road there had been a specified non-negative integer labeled onto it. What's more, for every city, we could find that all the roads starting from it had different labels. So with these labels, every city i could be expressed by the path with a series of numbers pi from the capital to it in order. But the book didn't say anything about how the roads were labeled and it seemed the way the roads were labeled was no longer known by anybody.

One day the king in the kingdom, Takamina, decided to relabel the roads so that the conditions in the story were satisfied. Also, a specified condition should also be met that the list of pi for cities without any road starting from it, namely {pathi}, was lexicographically minimized. To compare two lists, we first sort both lists separately and compare them as strings of paths (do as how strings compare while the elements of the strings are paths). To compare two paths, we also compare them as strings of numbers.

So you, the most talented programmer, can you solve the problem?

Input

There are multiple test cases. The first line of the input is an integer T ≈ 100 indicating the number of test cases.

For each test case, the first line are two integers n and Q (1 ≤ n ≤ 1000, 0 ≤ Q ≤ n). Next n - 1 lines each containing two integers u and v indicating a road from u to v. Next Q lines each containing one integer c (1 ≤ c ≤ n) indicating a query to the c-th smallest path in {pathi}.

Output

For each query, if there are less than c cities without any road starting from it, print "Out of range." (without quotes), otherwise, print the c-th smallest path in one line with labels in the path separated by one space. Print a blank line after each case.

Sample Input

1
8 5
0 1
0 2
0 3
1 4
2 5
3 6
3 7
1
2
3
4
5
Sample Output

0 0
0 1
1 0
2 0
Out of range.

  • 写回答

1条回答 默认 最新

  • devmiao 2017-04-09 15:43
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应