K - Iranian Hazfi Cup


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

The Iranian Hazfi Cup is a football tournament organized every year in a knockout format; i.e. the loser of each match is immediately eliminated from the tournament, and the winner gets to play in the next round. Every year, 2k2^k teams participate in this tournament (for some positive integer kk). All teams start the tournament in the first round and after each round, half of the teams that are still in the tournament are eliminated. The kthk-th round is the final round, where two teams compete for the championship. In total,2k12^{k-1} matches are held.

توضیح تصویر

The tournament bracket of the Hazfi Cup is determined ahead of time in the drawing ceremony in the presence of special guests. It determines which teams are facing each other in the first round, and which other teams they might encounter if they advance to the next rounds. Precisely, in the drawing ceremony, all 2k2^k teams are randomly mapped to the positions 1,2,,2k1, 2, \dots, 2^k in the first round as depicted in the figure for k=3k = 3.

The Iranian football federation must start organizing the Hazfi Cup 20232023. As many of the special guests might refuse to attend the drawing ceremony this year, the federation has decided to use the same tournament bracket as Hazfi Cup 20222022. Unfortunately, last year’s tournament bracket is not available, but all match results of last year’s tournament are available in an arbitrary order. It can be shown that the tournament bracket can be uniquely determined from these match results. Your task is to recover the tournament bracket from the match results of Hazfi Cup 20222022 in order to answer the following fans’ common questions for this year:

  • In which round two teams, AA and BB, may face each other?

ورودی🔗

The input starts with a line containing two space-separated integers kk, the number of rounds in the tournament (1k10)(1 \leq k \leq 10), and nn, the number of fans' questions (1n1000)(1 \leq n \leq 1000). The match results of the Hazfi Cup 2022 come in the next 2k12^k - 1 lines; one line for each result. Each match result is of the form:

  • teamA gAg_A - gBg_B teamB

where teamA and teamB are different non-empty strings of lowercase English letters of length at most 100, and gAg_A and gBg_B denote the number of goals scored by teamA and teamB, respectively (gAgB)(g_A \neq g_B). In the case of a draw, the winner is determined by penalty shootouts, and the match result is of the form:

  • teamA g(pA)g \, (p_A) - g(pB)g \, (p_B) teamB

where gg is the number of goals scored by each team during the main game, and pAp_A and pBp_B are the number of goals scored by teamA and teamB during the penalty shootouts, respectively (pApB)(p_A \neq p_B). The number of scored goals (i.e., gAg_A, gBg_B, gg, pAp_A, and pBp_B) are all non-negative integers less than 100. Note that each line denoting a match result in the input contains exactly 44 space characters.

The input ends with nn queries. Each query is given in a separate line containing two different team names delimited by a space character.

خروجی🔗

For each query in the input, print as the answer, a single integer in a separate line in the output.

مثال🔗

ورودی نمونه ۱🔗

2 3
perspolis 1(4) - 1(3) esteghlal
sepahan 0 - 2 perspolis
esteghlal 4(1) - 4(0) shamoshak
shamoshak sepahan
shamoshak perspolis
esteghlal shamoshak
Plain text

خروجی نمونه ۱🔗

2
2
1
Plain text
ارسال پاسخ برای این سؤال
در حال حاضر شما دسترسی ندارید.