Jarif_Rahman's blog

By Jarif_Rahman, history, 3 years ago, In English

I was trying to hack (my own solution) of DSU step 3 problem A and it got Unexpected verdict.

My Generator (spoilers are broken, so I am using pastebin)

I first searched it on google and found this blog, which says unexpected verdict means problems with authors solution. I hope this will get fixed (mentioning Aksenov239 for that).

Confusion about the constraint:

constraints for $$$n$$$ and $$$m$$$ is $$$\leq 2 \cdot 10^5$$$ in this problem. If I take $$$n = 10^5$$$, $$$m = 2\cdot10^5$$$ and in the first $$$\frac{n}{2}$$$ queries I connect the first $$$\frac{n}{2}$$$ sets and in the last $$$\frac{n}{2}$$$ I connect the last $$$\frac{n}{2}$$$ sets, then connecting $$$\frac{n}{2}$$$ and $$$\frac{n}{2}+1$$$ should take $$$\frac{n}{2}$$$ time. If I keep connecting $$$\frac{n}{2}$$$ and $$$\frac{n}{2} + 1$$$ and rollbacking for the rest of the queries then the time complexity should become $$$\frac{n^2}{4}$$$ and it should get TLE.

If there is a way to solve this please share that. (Cause I don't know how to do that >_< )

  • Vote: I like it
  • +22
  • Vote: I do not like it

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

I don't understand why it should take $$$O(n)$$$ time to connect if the size heuristic is used (and no path compression)

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

    If I connect the fist $$$\frac{n}{2}$$$ sets and the last $$$\frac{n}{2}$$$ sets, then there will be 2 sets, each of them of length $$$\frac{n}{2}$$$. So it should take $$$O(n)$$$ time to merge this two sets (or I made mistake to understand dsu, by the way — my dsu implementation).

    If I keep merging and rollbacking then the time complexity should become $$$O(n^2)$$$

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

      You shouldn't merge by changing the label of every item in the components, but by changing only the label of the component root that will be merged into the other root. If the size heuristic is used, the path from any vertex to its root should not exceed $$$O(\log n)$$$