Aoxoa's blog

By Aoxoa, history, 15 months ago, In English

How to solve such a problem: Given a set and a tree, it supports adding points to the set, deleting points, and querying whether the points within the set are connected blocks on the tree

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

are you talking about the "Union Find" data structure?

UnionFind
»
15 months ago, # |
Rev. 3   Vote: I like it +10 Vote: I do not like it

You can simply save the total number of connected blocks for maintenance. Because every point in the tree is a cut point, it is easy to calculate.

After adding a point x, the number of connected blocks increases by "1 — the number of points that Adjacent to x on the tree and in the set". For deleting a point, just reduce the same value.

Sample

Then, this problem is similar to Facebook hacker cup 2022 Qualification Round Problem D. Classify the relationship between the number of point degrees and the size of $$$\sqrt{n}$$$.

I'm not sure if I can do it faster, but I think it's hard.


Expand the tree into bfs order, and then only two intervals will be added (or subtracted) by one value for each operation. You can use a segment tree to solve it in $$$O(n\log n)$$$.