DSU with Component Size
core
Extend your DSU with a size(int x) query that returns how many elements are in the component currently containing x. Each node starts in a singleton component of size 1. unite(a, b) merges the components containing a and b (a no-op if they're already connected); afterward, size must report the merged count for every member of that component, not just its root.
Operations arrive as U a b (unite) or S x (print size(x) on its own line).
Example: DSU dsu(5); dsu.unite(0, 1); dsu.unite(1, 2): nodes 0, 1, 2 are now one component of size 3, so dsu.size(0) == 3. Node 3 was never touched, so dsu.size(3) == 1; merging elsewhere doesn't inflate untouched singletons.
Input format: each test in tests/*.in is laid out as:
- Line 1:
n q, the element count and the number of operations; elements are numbered0ton - 1. - Next
qlines: one operation each, eitherU a b(union) orS x(print the size ofx's component).
Test 03.in reads:
3 2
S 0
S 2
This is 3 elements with no unions at all, just two size queries; every component is still a singleton, so both print 1.
Where you'll use it:
✦ Solution & editorial unlock with the pass.