# MAGICHF - Editorial

Div1
Div2
Practice

Setter- \huge \frac{dK}{dc}
Tester- Teja Vardhan Reddy
Editorialist- Abhishek Pandey

CAKEWALK

### PROBLEM:

There is a gold coin under box at index X. S swaps are performed where two boxes are exchanged. We need to tell that finally, i.e. after all swaps are done, which indexed box has gold coin under it.

### QUICK-EXPLANATION:

Key to AC- Having coded 5-6 problems from cakewalk question is enough to solve this problem. Looping, Conditionals and swapping of variables is needed.

We initially know that gold coin is under box X. For each swap, we know the indices (i,j) of the boxes which are being swapped. We form basic conditions of form-

• Let Curr hold the index of box under which coin is initially. By the question, Curr=X initially. Now, for every swap, the following holds-
• If i\neq Curr and j \neq Curr- Do nothing. We dont care about such swaps.
• If i==Curr, then it means that box i and j are swapped, and coin was initially in box i. Now, after swapping, it will be under box at index j. Hence, we assign Curr=j.
• If j==Curr then by same reasoning we assign Curr=i.
• i==j==Curr , we need to do nothing. (I dont think this case exists in the question XD).

The final ans is in Curr. Please note I followed standard programming convention of = representing assignment operator and == representing “Check for Equality” operator.

### EXPLANATION:

This is one of the editorial where Quick Explanation more than sums up everything about the logic. Instead of making editorial unnecessarily lengthy by formulating the logic again, lets focus on implementation.

In case there is any doubt of logic under Quick Explanation, do ask. I kept it short as it was simply common sense. The gist of the logic is, if the coin is currently under any one of the bowls, i or j, then it will be in the other bowl with which we swapped after the operation. Keep a track of which bowl the coin is CURRENTLY in.

Taking the input is trivial, but with that being said, let me introduce you to Fast Input-Output methods. The rule of thumb says, if number of input is >10^5 or output >10^5, use fast input-output to save precious time. Else, many times correct solutions get TLE due to huge time wasted in IO operations.

Maintain a temporary variable to store answer. I used currAns. After this, implement the conditionals mentioned above. Remember that, for cases where (i \neq currAns \&\& j \neq currAns) and (i==currAns \&\& j==currAns) we dont need to do nothing, hence we ignore them. We only handle the remaining 2 cases. Implementation in tab below-

Click to view
   int currAns=x;
int a,b;
while(s--)
{
cin>>a>>b;//a corresponds to i, b corresponds to j. This line takes input
if(a==currAns)
currAns=b;//if (i==currAns) currAns=j;
else if(b==currAns)
currAns=a;//Same as above.
}
cout<<currAns<<endl;//Print


### SOLUTION

The code is pasted in tabs below for you guys to refer in case solution links dont work.

Setter

Tester

Editorialist

Click to view
/*
*
********************************************************************************************
* AUTHOR : Vijju123                                                                        *
* Language: C++14                                                                          *
* Purpose: -                                                                               *
* IDE used: Codechef IDE.                                                                  *
********************************************************************************************
*
Comments will be included in practice problems if it helps ^^
*/

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
#ifdef JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
srand(time(NULL));
int t;
cin>>t;
while(t--)
{
int n,x,s;
cin>>n>>x>>s;
int currAns=x;
int a,b;
while(s--)
{
cin>>a>>b;
if(a==currAns)
currAns=b;
else if(b==currAns)
currAns=a;
}
cout<<currAns<<endl;
}
return 0;
}


Time Complexity=O(S) per test case
Space Complexity=O(1)

### CHEF VIJJU’S CORNER 1. Swapping variables here and there kind of reminds me of the std::swap function of C++ STL. Its a nice function which comes in handy for beginners and professionals alike. It can swap variables, arrays, vectors, queues…you pretty much get it XD.

2. Why did Chef bother accepting Magicians challenge if he needs us to help him? -_-

3. Related Problems: Frankly, the entire beginner section is filled to brim with such level problems. Solve any random problem from there, EXCEPT

• Rupsa and Game
• Chef and Weird Game.
• One more Weird Game.