def find(x, parent):
    """
    This function gets a node x and the parent-part of a union find data structure.
    It returns the root of the tree that contains x. A tree compression technique called path halving is used.
    It is optimized with numba, using njit.
    """

    while x != parent[x]:
        #path compression (halving):
        parent[x] = parent[parent[x]]
        x = parent[x]
    return(x)