the_drunken_knight's blog

By the_drunken_knight, history, 3 years ago, In English

Problem: https://codeforces.net/contest/1620/problem/E

I was trying to apply some dsu-like logic in Problem E, when they asked for replacement of every $$$x$$$ with $$$y$$$ in the array, but it gave me WA on test 4.

The idea which I used during the contest:

from = replacement[x], to = replacement[y]
while(replacement[to] != to) to = replacement[to];
replacement[from] = to;

And, the idea which gave AC after the contest,

replacement[x] = replacement[y]

I know that the latter one is clever and more sleek, but why the former one fails? Can anyone please suggest a counter-case for it?

Except for these idea, the rest of code was exact same!

Submission 1 (with first logic): https://codeforces.net/contest/1620/submission/139872871
Submission 2 (with second logic): https://codeforces.net/contest/1620/submission/139872969

»
3 years ago, # |
  Vote: I like it -10 Vote: I do not like it

In the first code, by not recurring to the root of the tree that contains from, you omit to merge completely the two sets (the one containing x and the one containing y). Otherwise, even the code that gave you AC is very... sussy. Or at least, there is something in your code that you omit to show us (i.e. probably by changing from, you might rewrite some elements that oughtn't be rewrited?)

In any case, I do warmly, cordially, and everythingly reccomend you to read in more depth this page , so you would learn a.. proper implementation of this algorithm

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Sir, just want to re-mention that I'm not using a proper dsu logic, I'm using instead a dsu-like logic.

    A proper dsu logic, I think, will give wrong answer, because replacement of $$$x$$$, doesn't changes the replacement value of any other number $$$z$$$ in the group, unlike in dsu, where the parent of every member in the smaller group changes when union happens.

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it +1 Vote: I do not like it

      Please, don't do semantics on me.Using "dsu-like logic" is like saying building a cartesian tree uses "stack-like logic" or the Dinic's algorithm uses "bfs-like logic"

      As far as you let me know, on the following test:

      Spoiler

      So, if you want anyone to help you, you have to give us a little more context! Your code is clearly not wrong, since you claim that it passed all tests, but the picture of what you just shown me is incomplete. Please consider attaching the entire code.

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by the_drunken_knight (previous revision, new revision, compare).