Each of Farmer John's N cows (1≤N≤2⋅10^5) has a favorite color. The cows are conveniently labeled 1…N (as always), and each color can be represented by an integer in the range 1…N. There exist M pairs of cows (a,b) such that cow b admires cow a (1≤M≤2⋅10^5). It is possible that a=b, in which case a cow admires herself. For any color c, if cows x and y both admire a cow with favorite color c, then x and y share the same favorite color. Given this information, determine an assignment of cows to favorite colors such that the number of distinct favorite colors among all cows is maximized. As there are multiple assignments that satisfy this property, output the lexicographically smallest one (meaning that you should take the assignment that minimizes the colors assigned to cows 1…N in that order). Although i read the solution, i still cannot understand how the algorithm works? The solution:"If both cows b and c admire cow a then both b and c must have the same color. From now on, we can treat both b and c as the same cow; change all occurrences of c to b and merge the adjacency list of c into that of b. Repeat this process while at least two distinct cows admire the same cow. Once we reach a configuration in which a cow is admired by at most one cow this process terminates; we can just assign every cow a distinct color. If we always merge the smaller adjacency list of the two cows into the larger one then our solution runs in O((N+M)logN) time. We ensured that a few slow solutions did not pass but it is likely that many (not necessarily provable) heuristics passed anyways."