Disjoint sets (DSU)

Keep track of who belongs to which team as teams join up. This makes it easy to check whether two players are on the same team yet.

Process edges in insertion order. Parent pointers lead to the team captain. A search shortens the chain so the next lookup reaches the captain sooner.

Union by size and path compression take O((V + E) α(V)) time. The inverse Ackermann function α grows extremely slowly.