In many tutorials I’ve seen that parent array was initialized like this:
for(int i = 0; i < N; ++i) {
parent[i] = i;
size[i] = 1;
}
And then the find function was written like this:
int find(int x) {
while(x != parent[x]) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return parent[x];
}
In this video I see that parent was initialized with -1’s.
Could you please explain what is the main differences and in which situation those ways?