A rank problem
题号:NC226981
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 1024 M,其他语言2048 M
64bit IO Format: %lld

题目描述

Coach is fed up with sports rankings -- he thinks those who make up these bogus orderings are just nuts. In Coach's opinion changes in rankings should be evidence-based only. For example, suppose the th place team plays the st place team and loses. Why should the rankings be altered? The "worse" team lost to the "better" team, so nothing should change in the rankings. Put another way, there's no evidence that the ordering should change so why change it? The only time you change something is if, say, the th place team beats the st place team. NOW you have evidence that the rankings should change! Specifically, the st place team should be put directly below the th place team (we now have evidence that backs this up) and the teams in nd through th place should each move up one. The result is that the former st place team is now in th, one position below the team that beat it, the former th place team now in rd. Note that the relative positions of the teams now in st to rd place do not change -- there was no evidence that they should.

To generalize this process, assume the team in position  beats the team in position . If  then there should be no change in the rankings; if  then all teams in positions  should move up one position and the former team in position  should be moved to position .

For example, assume there are  teams initially ranked in the order  (best),  (worst). Suppose  beats . Then as described above the new rankings should become . Now in the next game played let's say  beats . After this the rankings should not change -- the better ranked team beat the worse ranked team. If in the next game  beats  the new rankings would be , and so on.

Coach was all set to write a program to implement this scheme but then he heard about ties in the English Premier League. The last we saw him he was standing motionless, staring out of his window. We guess it's up to you to write the program.

输入描述:

Input begins with a line containing two positive integers   () indicating the number of teams and the number of games played. Team names are  and initially each team  is in position  in the rankings (i.e., team  is in st place and team  is in last place). Following the first line are  lines detailing a set of games in chronological order. Each of these lines has the form   () indicating that team  beat team .

输出描述:

Output a single line listing the final ranking of the teams. Separate team names with single spaces.
示例1

输入

复制
5 3
T4 T1
T3 T1
T5 T3

输出

复制
T2 T4 T1 T5 T3
示例2

输入

复制
8 4
T4 T1
T1 T2
T2 T3
T3 T4

输出

复制
T1 T2 T3 T4 T5 T6 T7 T8