Union-Find(Without Rank) C++: Story
Harsh Mishra

Harsh Mishra @harshm03

About: Aspiring Software Developer | Tech Enthusiast | Skilled in HTML, CSS, JavaScript, Node.js, Express.js, React | Enthusiastic about Data Science and ML (NumPy, Pandas, scikit-learn) and DevOps tools.

Joined:
May 27, 2024

Union-Find(Without Rank) C++: Story

Publish Date: Aug 3
0 0

⚔️ Chronicles of the Shattered Crowns: The Union of the Realms

"When every warrior believes he is king, peace lies in illusion.
But when they learn who truly rules, the kingdoms begin to rise."

Ancient War-Rune of the Folded Lands


In the time before memory, the world was broken. Not by war, but by pride.
Every warrior, from the youngest squire to the oldest warlord, wore a crown and claimed a throne.
Every city stood alone, fortified against brothers who once rode together.
The earth was carved into N scattered realms, each oblivious to the others, each believing,

"I am the only king."

But in the shadows of time’s decay, a new age began —
not with blood, not with fire — but with understanding.


🏰 Class of Thrones: The Scroll of Inheritance

class UnionFind {
    vector<int> parent;

public:
    UnionFind(int n) {
        parent.resize(n);
        for (int i = 0; i < n; ++i)
            parent[i] = i;
    }
Enter fullscreen mode Exit fullscreen mode

The Scroll of Kingship was rediscovered.

A great Archivist, silent and steady, moved across the lands.
To every soul, from the depths of the desert to the edge of the ice, he handed a seal of truth:

“You are your own king… for now.”

And thus was the UnionFind class born, etched in steel and starlight.
The scroll, the parent array, began with each warrior pointing to himself.
They believed it was identity, but it was only potential.


🔍 The Pilgrimage to the Throne

    int find(int v) {
        if (parent[v] == v)
            return v;
        return parent[v] = find(parent[v]);
    }
Enter fullscreen mode Exit fullscreen mode

One day, a knight named Kael, bearer of sword and sorrow, questioned the world.

"If I claim kingship, who then is mine?"

He began a pilgrimage.
He rode to his parent, only to find that one too pointed onward,
and so he rode again… and again… chasing the line of crowns
until he reached a citadel where one man stood alone:

A true king, whose parent was himself.
The origin. The beginning. The root.

Kael fell to his knees and whispered,

“I have wandered far, so none after me must.”

And with that, he carved shortcuts into the earth,
connecting all paths he had walked straight to the king.
The Path of Compression was born, and it echoed in the find(v) spell forevermore.


🤝 The Pact of Blades

    void unite(int a, int b) {
        a = find(a);
        b = find(b);
        if (a != b)
            parent[b] = a;
    }
Enter fullscreen mode Exit fullscreen mode

In the Valley of Blades, two warlords stood eye to eye:
Irron of the East and Solun of the South.
Each called himself king. Each bore an army.

But the skies grew black. A storm whispered truths.
They sent riders — a and b — to the high towers of their realms,
and from there, the Archivist asked but one thing:

“Before you fight, look within. Who rules you truly?”

They cast the find() spell. They followed their own bloodline.
And when they reached their roots, they found separate thrones.

“Then one shall kneel,” said the Archivist.
“For unity is not surrender, but strength.”

The lesser king bent the knee. His kingdom’s parent was now the other.
The Pact was forged.
A unite(a, b) not in shame — but in permanence.


🧪 The Trials of the Herald

    bool connected(int a, int b) {
        return find(a) == find(b);
    }
};
Enter fullscreen mode Exit fullscreen mode

Across the continent, a lone Herald rode.

His task: to find if cities were still divided… or had they united under one king?

He whispered into sacred stones:

“Are a and b one realm?”

The winds carried his voice to the towers of find(a) and find(b).
If both banners hung from the same fortress, he would declare:

“They are one.”

But if not, the Herald would raise a red flag,
warning of dormant fractures.

Thus, he created the spell of truth: connected(a, b).


🌅 The Rise of One Realm

int main() {
    UnionFind realm(7);
    realm.unite(0, 1);
    realm.unite(1, 2);
    realm.unite(3, 4);
    realm.unite(5, 6);
    realm.unite(4, 5);
    realm.unite(2, 6);
}
Enter fullscreen mode Exit fullscreen mode

The scrolls say seven banners once flew alone.

Then…

  • The Flame-Born (0) took the hand of the Skyborn (1).
  • The Skyborn reached out to the Stonewalkers (2).
  • The Swornwood (3) formed bond with the Riverfolk (4).
  • The Deepforge (5) marched with the Frostspire (6).
  • Swornwood met Deepforge at the river’s edge and forged steel.
  • And in the final hour… Flame met Frost, as 2 and 6 joined hands.

The last sword dropped to the ground.

The Realms were One.

And in the great tower, the Archivist smiled,
for the parent scroll had finally become a tree with one root.


🧠 The Memory Seared in Flame

  • find(v) — the lone knight's path to truth; and his promise to shorten it for others.
  • unite(a, b) — the day two kings chose unity over blood.
  • connected(a, b) — the whisper that tells whether realms are still split.

The Archivist sleeps now, but the spells remain.

And when the next interviewer asks,
“Do you know how Union-Find works?”

You’ll not write code.
You’ll recount a saga.
One of kings, knights, and unity.

And with that, the kingdom — and the job — is yours.

Comments 0 total

    Add comment