Another task about trees#5347

Another task about trees

1 second
256 megabytes
Very hard

Consider a graph with vertices, initially devoid of edges. You are tasked with handling queries, which are of two types:

Solution

We will use the Union-Find data structure with path compression to efficiently manage these operations:

int n, m, l[NMAX];
int calc_leader(int v)
{
    if (l[v] != v)
        l[v] = calc_leader(l[v]);
    return l[v];
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        l[i] = i;
    for (int i = 1; i <= m; i++)
    {
        int x, y, z;
        scanf("%d%d%d", &z, &x, &y);
        if (z == 1)
            l[y] = x;
        else
            if (calc_leader(x) == calc_leader(y))
                printf("YES\n");
            else
                printf("NO\n");
    }
}

Your task is to design a test case that causes this solution to execute for an extended period. Specifically, the number of calls to the function calc_leader should be at least \frac{1}{4} M \log_2 M.

Input

The input consists of two integers and (1 \leq N \leq 10^6, 1 \leq M \leq 10^5, M \leq N).

Output

Produce lines of output. The -th line should be formatted as if the -th query is to add an edge from vertex to vertex , or if it is to check whether vertices and are in the same tree.

Examples

Example 1

Loading
Loading