A - Final Price


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

Neverland has recently experienced a rapid rise in the inflation rate. The value of money is continuously decreasing, and citizens’ purchasing power is lowered daily. The government is trying to control the inflation rate by testing various methods, such as reducing the amount of money in the economy by increasing interest rates and promoting investment, even in purchasing cars to be delivered in an unforeseeable future.

توضیح تصویر

Despite these efforts, the inflation rate is still above 50%50\%, and the prices are jumping up and down drastically every now and then. The Statistical Center of Neverland provides detailed information on the inflation rate and the average price change over time for a basket of goods commonly purchased by households. However, it is hard to calculate the actual price of a specific good at a given point in time using that information.

The ICPC (International Center for Price Changes) is asking you to help the citizens of Neverland to calculate the prices for their desired goods after a specific period of time. The information provided to you is the initial price of a good at the start of a time period, and the daily price change for that good over the observed time period.

ورودی🔗

The first input line contains an integer nn (1n10001 \leq n \leq 1000), indicating the number of days the prices are observed. The second line contains a positive integer indicating the initial price of the desired good on day 11. The next n1n-1lines contain n1n-1 integers showing the daily change in the price of the good, in order from day 22 to day nn. You can assume that the price of the good is always positive and never exceeds 1,000,0001,000,000 in the observed period.

خروجی🔗

In the output, print the final price of the good on day nn.

مثال‌‌ها🔗

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

2
11
68
Plain text

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

79
Plain text

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

4
110
-5
0
27
Plain text

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

132
Plain text

B - Flower Festival


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

Today is the Flower Festival day. The festival is held in Rose Square, at the end of Flower Street. People are heading towards the festival on Flower Street with n cars, numbered 11 through nn. Soroush, an expert traffic analyst, wants to know which car will arrive at Rose Square first. Using the traffic cameras on Flower Street, he has gathered the current location of all cars, along with their speeds. Each car maintains a constant speed throughout their journey. Also, the location of a car is defined as its distance from the start of Flower Street. Help Soroush find the first car that arrives at the festival. It is guaranteed that no two cars reach Rose Square at the same time.

ورودی🔗

The first line of input contains two space-separated integers nn (1n1001 \leq n \leq 100) and ff (1f10,0001 \leq f \leq 10,000), the number of cars and the length of Flower Street, respectively. The (i+1)th(i + 1)-th line (for 1in1 \leq i \leq n) contains the information of car numbered ii, two space-separated integers xix_i (0xi<f0 \leq x_i < f) and viv_i (1vi1001 \leq v_i \leq 100) indicating its observed location and speed, respectively.

خروجی🔗

Print the number of the car which will arrive at Rose Square first.

مثال‌ها🔗

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

3 200
0 1
10 5
40 1
Plain text

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

2
Plain text

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

5 100
0 1
10 3
60 2
75 1
10 4
Plain text

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

3
Plain text

C - Simplification


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

Amin records the price of his stock every now and then as a data point (ti,pi)(t_i, p_i) in his notebook, where pip_i is the price of his stock at day tit_i. The sequence of these data points represents a piecewise-linear function FF displaying the history of prices over a period of time. Indeed, FF connects every pair of consecutive points by a straight line segment. If the price is not recorded for some day tt, F(t)F(t) can be used as an estimate instead. His collected data is getting larger and larger as he has been tracking the price of his stock over a long period of time. Therefore, he has decided to reduce his data by throwing away some of his recoded data points and constructing a new piecewise-linear function FF′ with the remaining points. FF′ is a so-called “simplification” of FF. Amin wants to create the simplification in such a way that FF′ is a good approximation for FF. To this end, he has defined an error measure as follows.

Let FF be defined over a strictly increasing sequence L=t1,,tnL = \langle t_1, \dots, t_n \rangle of days, and FF' be defined over a subsequence L=t1,,tmL' = \langle t'_1, \dots, t'_m \rangle of LL, where t1=t1t'_1 = t_1, tm=tnt'_m = t_n, and F(ti)=F(ti)F'(t'_i) = F(t'_i) for 1im1 \leq i \leq m. (We call mm the size of FF'.) The error of FF' is defined as the maximum of F(tk)F(tk)|F'(t_k) - F(t_k)| for all 1kn1 \leq k \leq n.

For example, in the following figure, we have 66 data points, labeled 11 through 66, whose coordinates are the same as those presented in the second sample input, and FF' is a simplification of FF of size 33 with data points 11, 44, and 66. In this figure, FF is depicted by solid lines, and FF' by dashed lines. The error measure for FF' is realized by the vertical distance of point 22 to FF'.

توضیح تصویر

Amin’s goal is to minimize the size of FF' , while the error of FF' is bounded by a given value δ\delta.

ورودی🔗

The first line of input contains a positive integer nn (2n20002 \leq n \leq 2000) that shows the size of FF. Each of the next nn lines contains two integers tit_i, pip_i (1ti,pi1061 \leq t_i,p_i \leq 10^6), where pip_i is the price of the stock at day tit_i. The last line contains the error limit δ\delta which is a non-negative integer at most 10610^6.

خروجی🔗

In the output, print the minimum possible size of FF'.

مثال‌ها🔗

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

3
1 10
3 100
10 20
90
Plain text

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

2
Plain text

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

6
10 10
20 20
35 14
50 20
60 10
70 10
8
Plain text

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

3
Plain text

D - Dominoes


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

Pixar is gearing up to introduce its next animated film, Elemental, at the 20232023 Cannes Film Festival. But one of the scenes needs re-rendering. In this scene, there are nn dominoes in a straight line, and one of them is supposed to fall and drop a number of subsequent dominoes in a domino-like manner. Considering it is less than 11 month away from the 20232023 Cannes Film Festival, Pixar asked you to write a program that calculates the cost of re-rendering the scene for n initial domino choices.

توضیح تصویر

To simplify the calculations, we assume that the dominoes are displayed from the side like line segments with no width on the coordinate axis, and they fall to the left. They are numbered from left to right and domino ii has height lil_i and is located at xix_i. The cost of re-rendering this scene is equal to the area of the moving part of the scene, i.e. the union area of quarter circles of dropped dominoes. Note that domino falling areas may overlap, and the overlapped area should be calculated only once. The picture illustrates the falling of dominoes in the first sample test, when the initial domino choice for falling is the domino number 55.

ورودی🔗

The first line of input contains a positive integer nn, indicating the number of dominoes. The next nn lines, each consists of a pair of integers, xix_i and lil_i, indicating the location and the height of the domino number ii. It is guaranteed that n200,000n \leq 200,000 and 1xi1 \leq x_i, li109l_i \leq 10^9. Dominoes are given from left to right; i.e. xi<xi+1x_i < x_{i+1}.

خروجی🔗

Output nn lines, where the ithi-th contains the cost of re-rendering the scene with ithi-th domino as the initial falling domino. All numbers must have an absolute or relative error of at most 10610^{-6}.

مثال🔗

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

7
2 2
5 3
9 5
12 1
13 5
14 2
16 3
Plain text

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

3.1415926536
7.0685834706
24.6597736956
0.7853981634
42.2509639206
44.1641868756
49.7141240520
Plain text

E - Parking Party


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

Peyman has decided to host a dinner party in his house at Zabol. His house has a rectangular parking lot which can be represented by a grid with nn rows and mm columns. A car can park in one of the n×mn×m cells in this grid. However, some cells are occupied by immovable pillars and thus, no car can park in or pass through them. Each row or column in the parking lot has an entrance on both its ends. When a car enters the parking lot through an entrance, it can just move forward straightly in the corresponding row or column; it stops and parks in a grid cell when it reaches the opposite entrance or when the next cell is occupied by a pillar or another parked car. Additionally, a car cannot enter a row or column if its first cell is already occupied.

Peyman wants to maximize the number of cars that can be parked in his parking lot. In order to do that, he can instruct the guests on which entrance to take upon arrival. Your task is to help Peyman achieve this task.

ورودی🔗

The first line of input contains two space-separated integers nn and mm (1n,m10001 \leq n, m \leq 1000), the number of rows and columns in the parking lot, respectively. Each of the following nn lines contain a string of length mm made of . and o characters. the jthj-th character of the (i+1)th(i + 1)-th line is an o if the cell in row ii and column jj contains a pillar, and it is a . if it is empty.

خروجی🔗

Print a single integer kk, the maximum number of cars Peyman can park in his parking lot.

مثال‌ها🔗

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

3 3
.o.
o.o
.o.
Plain text

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

4
Plain text

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

3 4
oooo
....
...o
Plain text

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

7
Plain text

F - Ammunition Storage


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

The Do-Barareh military area is like an n×mn×m grid, each cell of which has a specific height. The commander of this military area is looking for a rectangular sub-area of this area, with width and height least 22, whose its four corner cells are higher than the rest of its cells. He plans to install watchtowers in the corners of this sub-area to monitor the entire sub-area and use it for ammunition storage. Your job is to help the commander to find out how many valid sub-areas there are to choose as the ammunition storage. You can assume cell heights are distinct.

ورودی🔗

The first line of input contains two space-separated integers nn and mm (2n,m7502 \leq n, m \leq 750). Each of the next nn lines contains mm space-separated integers showing the cell heights. It is guaranteed cell heights are distinct numbers between 11 and nmnm (inclusive).

خروجی🔗

Print the number of valid sub-areas to be used as an ammunition storage.

مثال🔗

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

3 3
9 4 8
2 1 3
7 5 6
Plain text

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

7
Plain text

G - Laboratory Report


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

You are assigned to write a software for a medical laboratory. A portion of the software is about generating reports on the conducted medical tests. A medical test consists of a series of measurements on specimens (like blood or urine) sampled from a patient. Each measurement has a name, a unit, and a result (value). For example, the “Cholesterol” in a blood sample might be 180mg180 mg/dldl. Additionally, each measurement is attached with a reference table. A reference table is a general set of rules classifying a (status) flag for the patients based on their measurement results. For example, if “Triglycerides” has a result in the range [10,190][10,190], then it is classified as “normal”; and it is flagged as “low” or “high” if it has a result below or above this range, respectively.

Given all the measurement reference tables and the results for several patients, you have to generate the report of their medical tests.

ورودی🔗

A text in the input specification of this problem is a non-empty string of arbitrary printable ASCII characters, including the space, punctuation, and alphanumeric characters, but not the newline character. It is guaranteed that a text does not start or end with the white space characters. In addition, all real numbers in the input are in decimal format with at most 77 characters, and their absolute value will not exceed 10710^7.

The input consists of multiple (at least 22 and at most 100100) sections. Each section ends with a single line containing 8080 consecutive = characters.

The reference tables are described in the first section. There are at least 11 and at most 100100 reference tables, and the lines of two adjacent tables are separated with a single line containing 7575 consecutive - characters. The first line of a reference table contains the measurement name, a text of length at most 2525, which is unique among all the reference tables. The second line contains a text of length at most 1515 as the measurement unit. The remaining of the reference table describes its flag classification rules and is either a single line, or a set of pairs of lines. In the former case, the single line specifies the criteria for classifying the result as “Normal”. In this case, if the measurement result does not meet this criteria, it is classified as “Abnormal”. In the latter case where the remaining of the reference table is in the form of a non-empty set of (at most 1010) pairs of lines, the classification rules are more complex. In this case, the first line of each pair specifies the classification criteria, and the second line contains a text of length at most 2525 as the flag name preceded by two space characters. In any of the cases mentioned above, the classification criteria is a text of length at most 50 in one of the following forms (AA and BB are real numbers, and xx is a measurement result):

Criteria format Meaning
<A< A x<Ax < A
A\leq A xAx \leq A
>A> A x>Ax > A
A\geq A xAx \geq A
[A,B][A, B] or ABA \sim B AxBA \leq x \leq B
[A,B)[A, B) Ax<BA \leq x < B
(A,B](A, B] A<xBA < x \leq B
(A,B)(A, B) A<x<BA < x < B

There might be arbitrary number of spaces between the different parts of a text specifying a classification criteria. So for example, [2,5][2,5], [2,5][ 2 , 5], and 2 52~ 5 are valid and equivalent texts. When the flag classification rules of a reference table are described with a set of pairs of lines, it is guaranteed that the measurement value space is correctly partitioned by the given set of classification criteria; i.e., each possible measurement result matches exactly one of the provided classification criteria.

The next sections in the input specify the measurement results for the patients. The first line of each section contains a text of length at most 6060 as a patient’s name. Each of the next lines describes a measurement result for that patient with the measurement name and value (a real number) separated with an arbitrary number (between 11 and 2020) of space characters. There are at least 11 and at most 100100 measurement results for a single patient. Each measurement name appears at most once in the measurement results of a patient.

خروجی🔗

For each section in the input except the first section (which describes the reference tables), you shall write the laboratory test report of its corresponding patient followed by a single line containing 8080 consecutive = characters. A report consists of a line containing the patient’s name and a table depicting the test results. The table format is illustrative in the sample output. If there were kk measurements in the corresponding input section, this table should have k+4k+4 lines of exact length 7575. All the empty areas shall be filled with the space character. The table has 44 columns:

  • Test: The measurement name; starting at the beginning of the line (the 1st1-st character).
  • Result: The measurement result value, printed in the exact format as the input; starting at the 27th27-th character of the line.
  • Unit: The measurement unit; starting at the 35th35-th character of the line.
  • Flag: The classified flag based on the measurement result and the reference table; starting at the 51st51-st character of the line. Nothing (but space characters) should be written in this cell if the flag is “Normal”.

The measurement rows in the output table should be printed in the same order of appearance as in the corresponding input section. In addition to the measurement rows, the table should have a heading line and three separator lines (containing 7575 consecutive - characters) as depicted in the sample output. Notice: The solutions for this problem are judged very strictly. In order for your output to be considered correct, all its characters, including the white space or upper/lower case letters, must exactly match the expected output.

مثال🔗

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

Cholesterol
mg/dl
130~200
---------------------------------------------------------------------------
HDL-chol
mg/dl
>= 55
---------------------------------------------------------------------------
Fast Blood Sugar
mg/dl
<70
  Hypoglycemia
70 ~ 99
  Normal
(99, 126)
  Impaired (Prediabetic)
>= 126
  Diabetic
---------------------------------------------------------------------------
Secret Poison
mg/L
<   0.1
  Noneffective
0.1 ~0.5
  Mild
(   0.5  , 1.5   ]
  Moderate
(1.5,5)
  Severe
[5, 10]
  Lethal (non-instant)
>   10
  Instant Killer
================================================================================
Mr. Fat
Fast Blood Sugar 130
Cholesterol      230
HDL-chol          55
================================================================================
413%3! 4n@+01!3\/!(# /\/@\/@1n`/
Secret Poison   3.8
Cholesterol     150
================================================================================
Y@1d@ 49#@|=@21!
Fast Blood Sugar 90
Secret Poison     7.9
===============================
Plain text

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

Mr. Fat
---------------------------------------------------------------------------
Test                      Result  Unit            Flag                     
---------------------------------------------------------------------------
Fast Blood Sugar          130     mg/dl           Diabetic                 
Cholesterol               230     mg/dl           Abnormal                 
HDL-chol                  55      mg/dl                                    
---------------------------------------------------------------------------
================================================================================
413%3! 4n@+01!3\/!(# /\/@\/@1n`/
---------------------------------------------------------------------------
Test                      Result  Unit            Flag                     
---------------------------------------------------------------------------
Secret Poison             3.8     mg/L            Severe                   
Cholesterol               150     mg/dl                           
Plain text

H - Network Topology in Hezardastan


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

Hezardastan, a leading information technology group in Iran, has a huge data center containing nn servers and mm terminals (where mnm \leq n). A terminal is a pair of keyboard and monitor that can be connected to a server for administrative purposes. The servers are numbered 11 through nn and the terminals are numbered 11 through mm. This data center has a network topology in which not every terminal is necessarily able to connect to every server. For example, the figure below depicts 33 terminals and 66 servers where a terminal can connect to a server if a line is drawn between them.

توضیح تصویر

A subset SS of the servers with size mm is called manageable if its members are allowed by the network topology to be simultaneously managed by the terminals, i.e. each terminal can be connected to a distinct server in SS. For example, the subset {2,3,6}\{2, 3, 6\} in the example above is manageable as its members can be respectively managed by the terminals {1,2,3}\{1, 2, 3\}. A subset of the servers is called unmanageable if it has size mm and is not manageable. A network topology is called totally manageable when it causes no unmanageable subset of servers. For example, the network topology shown in the example above is totally manageable, but if the connection link between terminal 22 and server 55 is removed, then it will not be totally manageable anymore since the subsets {1,5,6}\{1, 5, 6\}, {2,5,6}\{2, 5, 6\}, {3,5,6}\{3, 5, 6\}, and {4,5,6}\{4, 5, 6\} will become unmanageable. Given a network topology for the data center, you have to find if it is totally manageable or it makes an unmanageable subset.

ورودی🔗

The first line of input contains two integers mm and nn separated with a single space (1m150,1n400,mn)(1 \leq m \leq 150, \, 1 \leq n \leq 400, \, m \leq n). The next mm lines describe the network topology by an m×nm \times n matrix. Each of these lines contains nn space-separated integers which are either 00 or 11. The jj-th number (for 1jn1 \leq j \leq n) in the (1+i)(1 + i)-th line of input (for 1im1 \leq i \leq m) is 11 if terminal ii can connect to server jj, and it is 00 otherwise.

خروجی🔗

If the given network topology is totally manageable, you only have to print 11 in the first line of output. Otherwise, you should print 00 in the first line of output and an unmanageable subset of servers in the second line in the form of mm space-separated integers (indicating the server numbers, in any arbitrary order). If there are multiple unmanageable subsets, you can print any one of them.

مثال‌ها🔗

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

3 6
1 1 1 1 0 0
0 1 1 1 1 0
0 0 1 1 1 1
Plain text

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

1
Plain text

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

3 6
1 1 1 1 0 0
0 1 1 1 0 0
0 0 1 1 1 1
Plain text

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

0
1 5 6
Plain text

I - Windcatchers


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

Yazd is the city of windcatchers. There are several famous and historical windcatchers in Yazd, including the windcatcher of Dowlatabad Garden, which is the tallest one in the world. The new mayor of Yazd is going to celebrate the history of windcatchers. To this end, he has decided to construct a new highway in the city with the following properties:

  • The highway is composed of two ways of the same width.
  • The two ways are adjacent, with a straight line in common (which we call the middle line).
  • The highway must not pass through any windcatcher. However, windcatchers may lie on the boundary of the ways, including on the middle line.
  • To celebrate the windcatchers, the highway must have at least two windcatchers exactly on its middle line.

توضیح تصویر

The mayor wants to construct a highway of maximum possible width satisfying all the above conditions. However, due to a large number of windcatchers in the city, finding the best place for such a highway is not an easy task. As such, the mayor has decided to hire you to find the best location for constructing the highway. To simplify things, each windcatcher is represented by a single point in the plane. Moreover, we may assume that the constructed highway has infinite length. An example of a highway of maximum width among a set of points (windcatchers) is illustrated in the above figure.

ورودی🔗

The first line of input contains a positive integer nn, indicating the number of windcatchers. The next nn lines, each consists of a pair of integers, xx and yy, indicating the location of a windcatcher in the city. Note that line i+1i +1contains the location of windcatcher ii. For simplicity, we assume that each windcatcher is a point in two dimensions, and no two windcatchers lie on the same point. Moreover, we assume that there are at least three noncollinear windcatchers in the city. It is guaranteed that 3n4,0003 \leq n \leq 4,000 and 0x,y1090 \leq x,y \leq 10^9.

خروجی🔗

Print a float number indicating the width of a maximum-width highway satisfying all the required conditions. Your output will be considered correct if its absolute or relative error is at most 10910^9.

مثال‌ها🔗

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

3
0 0
0 1
1 0
Plain text

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

1.000000000000
Plain text

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

4
15 18
11 20
20 9
7 8
Plain text

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

9.356972863938
Plain text

J - Magic with Cards


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

Mahsa has been practicing shuffling cards for a few months now. Tonight, she finally decided to invite her friends over and show off her new skills. So she picks up a deck with 2n2n cards, shows her friends the face of the cards without changing the deck order and asks someone to pick two positions ii and jj in the deck. Then, she tells everyone that she is going to move the card in the ii-th position to the jj-th position by applying only two types of shuffles.

Assume the cards in the deck are c1,c2,,c2n\langle c_1, c_2, \dots, c_{2n} \rangle. Mahsa can perform these two shuffles as many times as she wants:

Riffle: Divide the cards into two parts c1,,cn\langle c_1, \dots, c_n \rangle and cn+1,,c2n\langle c_{n+1}, \dots, c_{2n} \rangle and produce c1,cn+1,c2,cn+2,,cn,c2n\langle c_1, c_{n+1}, c_2, c_{n+2}, \dots, c_n, c_{2n} \rangle.

Scuffle: From c1,c2,,c2n\langle c_1, c_2, \dots, c_{2n} \rangle, produce c2,c1,c4,c3,,c2n,c2n1\langle c_2, c_1, c_4, c_3, \dots, c_{2n}, c_{2n-1} \rangle.

Help Mahsa find out the minimum number of shuffles she needs, and she’ll figure out the rest.

ورودی🔗

The input consists of a single line containing three space-separated integers nn, ii and jj (1n1051 \leq n \leq 10^5 and 1i,j2n1 \leq i, j \leq 2n).

خروجی🔗

Print a single integer, the minimum number of shuffles required to bring the ii-th card to the jj-th position. If it is not possible to do so, print 1-1 instead.

مثال‌ها🔗

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

4 3 8
Plain text

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

3
Plain text

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

5 4 1
Plain text

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

5
Plain text

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

1 1 1
Plain text

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

0
Plain text

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