In armyland there exist a gathering of individuals named ProgrammersArmy who are eager to come up as some assistance for the residents of armyland as due to Coronavirus individuals are not permitted to escape their homes because of which basic things are not provided to them. 

So ProgrammersArmy thought of an answer for beating this circumstance, The arrangement was quite basic, they chose to assemble data about the houses in the armyland and the street with which they are associated. 

we should signify the all out number of houses in the armyland with N. 

Yet, while assembling such data they saw a theme in the separations between each house suppose house An and house B have a separation which is limit of An and B. 

Yet, the military came to realize this isn’t adequate data for making their model spread the whole city for conveying things effectively. Subsequently, they chose to accumulate the data that contains the subtleties having the base separation between each house with each other house in the city. 

The military chose to some way or another scramble this data with the goal that it won’t be abused. So the encryption calculation which the military followed is as appeared underneath how about we name this calculation as “legend”, where Ans shows the last encoded number and n means our unique number : 


Calculation legend: Instate Ans: = 0 On the off chance that whole number n isn’t zero Do bitwise and with (n−1) and allocate the incentive back to n i.e n: = n and ( n – 1 )n and ( n – 1 ) 

Augmentation Ans by 1 go to stage 2 Else bring Ans back The information that is assembled by a group already is presently scrambled in a specific manner for example the base separation of each house with different houses is passed to our “legend” and the Ans for example returned by the “legend” is done bitwise OR with the subsequent worth got as passing the base of all separations in that chain of houses to “legend”. 

Note: here the expression “that chain of houses” in above proclamation relate to a rundown that contains least separation of one house with each other house. 

In the wake of scrambling information the military presents this model to head of the military, who questions on the given information which is contained in the model put together by the military and you are needed to answer those inquiries. 

Note: The houses are numbered from 1 to N as portrayed previously. 



The primary line of information contains a solitary number T, the absolute no. of experiments. The portrayal of T experiments follows. 

The principal line of each experiment comprises of two space-isolated whole numbers N, M signifying the complete number of houses, and the quantity of streets individually. 

Next M lines contain two space-isolated A, B indicating that there is a street among An and B. 

The following line contains Q indicating the quantity of questions. 

The following Q lines contain two space-isolated whole numbers h1, h2 meaning the quantity of the house individually as portrayed previously 



For each inquiry print a solitary number in the new line signifying the response to the question ( i.e the scrambled separation between house h1 and house h2 ) as contained in the encoded model. 

Note : Output of each experiment ought to be imprinted on new line. 









Test Input: 

6 9 

1 2 

1 6 

2 6 

2 3 

3 6 

3 5 

3 4 

4 5 

5 6 

1 2 

2 5 

6 4 


Test Output: 



As by building the model and scrambling it with the calculation as portrayed in the inquiry above you will find that the base separation between houses is as per the following : 

house 1 and house 2 have a base separation of 1 

house 2 and house 5 have a base separation of 1 

house 6 and house 4 have a base separation of 2 

house 3 and house 3 have a base separation of 0



March Long Challenge 2021 Solutions

April Long Challenge 2021 Solutions

Codechef Long Challenge Solutions

February Long Challenge 2021

1. Frog Sort Solution Codechef

2. Chef and Meetings Solution Codechef

3. Maximise Function Solution Codechef

4. Highest Divisor Solution Codechef

5. Cut the Cake Challenge Solution Codechef

6. Dream and the Multiverse Solution Codechef

7. Cell Shell Solution Codechef

8. Multiple Games Solution Codechef

9. Another Tree with Number Theory Solution Codechef

10. XOR Sums Solution Codechef

11. Prime Game Solution CodeChef

12. Team Name Solution Codechef

January Long Challenge 2021

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS


Related :

Related :

Leave a Comment

eleven − four =