Блог пользователя VladiG

Автор VladiG, история, 4 недели назад, По-английски

First of all, this technique isn't anything new, it's a fairly known one, but I couldn't find any resources on this topic other than this comment which is somewhat hard to find, so I decided to write a full blog.

With that being said, let's begin with the blog.

The problem

Lets consider the following problem: A graph is given with $$$N$$$ nodes and $$$Q$$$ queries. Each query is one of the following:

  • Add edge $$$(u,v)$$$
  • Remove edge $$$(u,v)$$$
  • Query the number of connected components in the graph

The timeline

Lets imagine a timeline, from time $$$1$$$ to time $$$Q$$$, at each moment either some edge gets added, removed, or a query gets asked.

Now the main idea of this technique is instead of dealing with deletions, for each edge we determine some intervals during which it exists. Namely if edge $$$(u, v)$$$ gets added at time $$$A$$$ and then later removed at time $$$B$$$ then that edge will obviously exist in the time frame $$$[A, B)$$$.

The Segment Tree on time

Now we've turned the problem into a range update problem, which suggests using a segment tree. We build a segment tree over the timeline. For each node, we store a list of edges that are active throughout the segment represented by that node.

Each interval gets decomposed into $$$O( \log Q)$$$ segments in the tree, and for each of those segments, we simply add the corresponding edge to the node.

After building the tree, we will run a DFS on it while maintaining a DSU with rollbacks (this is a well-known structure that can handle operations in $$$O(\log N)$$$).

At each node, we add all the edges in the node to the DSU, recurse downwards, and then rollback.

At the leaves, if the time corresponds to a query we can easily answer it since our DSU is already built.

This way we will handle everything offline in $$$O(Q \log Q \log N)$$$.

How is this useful?

You may be wondering by this point how this is useful at all since Dynamic Connectivity can be solved both online and offline in a better time complexity.It turns out this technique can be generalized.

Notice that the only part specific to the problem that we used in our solution is the Rollback DSU. So this technique can be applied in any problem with addition and deletion of pretty much anything to any structure. The only condition is that we need to be able to handle addition, and most importantly, rollbacks in a good time complexity.

In return for writing this amazing blog I am asking you to link any problems that can be solved with this technique in the comments.

Big thanks to EntityPlantt for using his grammar police abilities for once to not ragebait me and instead help me fix my terrible grammar.

  • Проголосовать: нравится
  • +121
  • Проголосовать: не нравится

»
4 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
4 недели назад, скрыть # |
Rev. 2  
Проголосовать: нравится +20 Проголосовать: не нравится

As a grammar policeman, I am impressed by my fixing

»
4 недели назад, скрыть # |
 
Проголосовать: нравится +32 Проголосовать: не нравится

but I couldn't find any resources on this topic other than this comment

?

»
4 недели назад, скрыть # |
Rev. 2  
Проголосовать: нравится +5 Проголосовать: не нравится

I have solved this https://mirror.codeforces.com/contest/981/problem/E question with same technique. I suggest everyone to give it a try.

My solution: https://mirror.codeforces.com/contest/981/submission/272892239

»
4 недели назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

OMG! Finally an educative, amazing, beautiful, excellent and fabulous blof on this platform!

»
4 недели назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

but I couldn't find any resources on this topic other than this comment

There is this blog

»
4 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi, great blog! Can you please recommend some cool hard problems that can be solved with this tech?

»
4 недели назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

I've been truly blessed to find this blog! The idea of doing things on time instead of arrays and similar extends my smile to the ears. To thank you for reminding me of my favourite topic, I will publish some links of problems using this incredible solution.

1681F - Unique Occurrences

981E - Addition on Segments

601E - A Museum Robbery

1140F - Extending Set of Points

2104G - Modulo 3

I hope this brings you the maximum amount of happiness, for this blog did that to me!

»
4 недели назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Dynamic Connectivity can be solved with Link-Cut trees online in $$$O(Q \log N)$$$

How? To my knowledge, this is only possible under the additional constraint that the graph is always a forest. In the general case, the complexity is only $$$O(\log^2)$$$ per query, and you need a additional sophisticated algorithm on top of Link-Cut tree.

»
4 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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