SORTVS - Editorial

Check out my comment.

Can you add more comments in your code please. Like why you made a structure , what is the function dfs doing, is it an abbreviation for Depth First Search. I am a beginner and it’s still hard for me to comprehend the editorial and their solution.

1 Like

Thank You :slight_smile:

Can you please elaborate your approach.
Thankx in advance

I have commented for almost everything upto my knowledge except the DSU part.
You can read about DSU (Disjoint Set Union ) in
https://cp-algorithms.com/data_structures/disjoint_set_union.html

Sure , here is my submission : CodeChef: Practical coding for everyone

Just to save you some time, first i call the function “addpairs” to add “new free swaps” from the connected component.
Then I call the function “transform” , to enlarge the cycle (I used DSU) using only free swaps. And then for each cycle I used dp to calculate the answer.

Also, I think that the same elements will be in the enlarged cycle no matter how you enlarge it, but the order of this elements can be different and that could change your possibilities for the splitting process. At least in the way I implemented, I think this can return a wrong answer.

My code will fail in this case :
1
17 9
1 15 14 3 4 13 5 17 8 9 12 2 11 6 16 7 10
8 14
10 11
3 6
4 6
10 13
2 14
11 17
7 9
1 16

The correct answer is 9. And my code outputs 10 . Although, if I randomized the process of enlarging the cycle, it could get the correct answer (9) . That’s why I think that the order matters.

I think the way you have implemented addPairs, the function is not adding all the pairs, which are possible. you can use dfs or bfs to find all the connected nodes (through pairs) and then add all of them as pairs.
let’s chat over hangout and not spam this discuss thread.
my email id: ashok1113@gmail.com

can you provide your code link

@amrit5
Sure.
https://www.codechef.com/viewsolution/32841371

1 Like

CodeChef: Practical coding for everyone i need cases where my code fails.and want to know was my approach correct?

@harisisbatman Sir,sorry to disturb you but can you explain me the actions of ‘splitting the cycles’,‘finding maximum number of disjoint cycles’, that you have done? I am not able to understand also how bitmask dp is helping us…
I would be grateful if you can kindly help this beginner…
Thanks in advance

1 Like

Sir, can you explain in a bit detailed manner what is all this enlarging of cycles you are talking about?

sir can explain code by some comment

1 Like

@rajarshi_basu Sir,can you kindly explain why the setter’s solution is returning character in the get function()? and what is this char &ans in get function?And also why are we considering that total edges will be 18 max??Because n vertices can have nC2 edges here where every edge represents a possible swap and so maximum edges can be 18C2…
Anyone else also kindly help me if you have any knowledge of the above questions?
Thanks in advance

@sayan_1999 @amrit5

  1. I inputted the robot_graph (Hope its is clear to you what I am doing with the input and how I have structured the indices of vases that the robot can swap into a graph).
  2. I did DFS on the robot_graph to set the representatives. By this, I mean finding the connected components. For example, if 1, 2 and 4 are connected then I will set rep[1] = rep[2] = rep[4] = 1. This helps me in identifying which vertex belongs to which connected component.
  3. I generated the new compressed graph where each vertex now represents a single connected component. You don’t have to understand each and every line of my code, you should just know what I am doing. (As a matter of fact, I myself don’t know how I am doing this step in my code. I will have to analyze it). But you should think about it yourself how you can do it.
  4. The most important part is detecting the maximum number of cycles. I don’t actually know what bitmask dp means. And you don’t need to know it either to understand my solution.
    I have used repeated BFSs to do this task. I believe you can also do it with DFSs. The whole idea is to start the search from the source vertex until I find a cycle. Once I find a cycle. I delete this cycle from the graph and recursively calls the same function on the same graph to detect the rest of the cycles. It returns me how many more cycles I can have if I chose to delete that specific cycle at that specific time.
    After that, I add that cycle back in my graph and I continue my BFS to detect any more cycles.
    I’ll explain it again.
    In my function, I take two parameters, the source, and the graph. In one function execution, I find all the cycles which start from the source and end at source in the given graph. Whenever I find any such cycle, I delete it, calls the function recursively for the remaining graph, I add it back again and keep my search running to look for any other cycles which are also starting from source and ending at the source.

On a side note, sir, please don’t call me sir, I appreciate the gesture but it really feels awkward to me :rofl:

5 Likes

:sweat_smile:ok

@harisisbatman It’s so kind of you to explain your approach on our request.I would like to know that
Why dont we just simply count total number of edges in the component (node) graph to get the total cost?

Took the same approach. And this exactly is what the bitmask dp is invariably doing but in a different way. The key is to find the maximum number of cycles. The approach is identifying all disjoint cycles of all sizes starting from shortest to the longest. But this still isn’t enough to find the maximum because in order to do that we need to the choose right of cycles before the rest. In order to account this, we find number of cycles taking each node as starting source. This accounts for the need to choose the right cycle before the other cycles.

1 Like

This is because, if you try a few examples on paper, you will find that for every cycle of n edges, you only need to swap n-1 elements and not n. So, to minimize the no. of swaps, you must find the maximum number of cycles.
You can see this in this simplest example- suppose the given permutation is 2 1. In the graph for this permutation, you will have an edge from first node to the second and one from the second node to the first. So there are two edges, but you only need 1 swap to get in to the desired state because those two edges are actually forming a cycle.

I feel like the complexity of the author’s bitmask solution is kind of dubious. Each cell of the dp has N transitions, which makes the complexity O(N^3 * 2^N). Of course in practice, most cells of the dp probably aren’t visited which is why the solution passes under the time limit quite comfortably, but it feels kind of unsatisfying to not have proof initially.

Does anybody have some proof that say for instance, half the cells aren’t visited? (in which case N^3 * 2^(N - 1) < 1e9, which gives us a lot more confidence that the solution will pass)