eolymp
bolt
Try our new interface for solving problems
Problems

Soccer

Soccer

In soccer, there are many different rewards (and punishments) depending on how you rank in the league at the end of a season. For example, in the British Premier League, the top \textbf{4} teams are eligible to play in the Champions League, the next team is eligible to play in the Europa League, and the bottom three teams are relegated to the lower division. It is now near the end of the soccer season, and there are still a number of games to be played. For any given team, we wish to know what is the highest and lowest rank it can have at the end of the season. For each game played, a team wins if it scores more goals than its opponent. A team loses a game if it scores fewer goals. When both teams score the same number of goals, we call it a draw. A team earns \textbf{3} points for each win, \textbf{1} point for each draw and \textbf{0} point for each loss. Teams are ranked according to the number of points earned (more points result in a higher ranking). Teams that are tied are given the same rank. For example, if two teams are tied and have the next highest point total after the \textbf{3}rd place team, then they are both ranked \textbf{4}th (and the next team is ranked 6th). In real life, factors such as goal differences and goals scored are used to break ties, but we will not consider these for this problem. You are given a list of soccer teams and a list of matches in a season. You may assume that every team will play the same number of games at the end. Some of the matches have been played and the results are known. \InputFile The input consists of a number of cases. The first line in each case specifies two integers \textbf{n} and \textbf{m} (\textbf{2} ≤ \textbf{n} ≤ \textbf{20}, \textbf{1} ≤ \textbf{m} ≤ \textbf{1000}) indicating the number of teams in the league and the number of matches in the season. The next \textbf{n} lines contain the name of each team in its own line. The team names contain only alphabetic characters and have lengths at most \textbf{30} characters. This is followed by \textbf{m} lines each of the form \textbf{team1 vs team2: x y} with \textbf{team1} and \textbf{team2} being the names of two different teams, and \textbf{x} and \textbf{y} are non-negative integers (or both are \textbf{-1}), indicating that in the game between \textbf{team1} and \textbf{team2}, \textbf{team1} scores \textbf{x} goals and \textbf{team2} scores \textbf{y} goals. If both \textbf{x} and \textbf{y} are \textbf{-1}, then the game has not yet been played. At most \textbf{12} games will not have been played yet. The input is terminated with \textbf{n = m = 0}. \OutputFile For each team in the same order as the team list in the input, print one line of the following form: \textbf{Team XXX can finish as high as nth place and as low as mth place.} Use \textbf{st}, \textbf{nd}, and \textbf{rd} instead of \textbf{th} for first, second, and third place, respectively. Print a blank line between cases.
Time limit 10 seconds
Memory limit 64 MiB
Input example #1
4 6
ManUnited
Arsenal
Chelsea
Tottenham
ManUnited vs Arsenal: 3 1
Chelsea vs Arsenal: 2 2
ManUnited vs Chelsea: 1 0
Tottenham vs ManUnited: -1 -1
Tottenham vs Chelsea: 0 4
Tottenham vs Arsenal: -1 -1
0 0
Output example #1
Team ManUnited can finish as high as 1st place and as low as 1st place.
Team Arsenal can finish as high as 2nd place and as low as 4th place.
Team Chelsea can finish as high as 2nd place and as low as 3rd place.
Team Tottenham can finish as high as 1st place and as low as 4th place.