Cod sursa(job #2142340)

Utilizator inquisitorAnders inquisitor Data 24 februarie 2018 22:31:23
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int t[100001];

int root(int x)
{
    int y = x, next;

    while(t[y])
    {
        y = t[y];
    }

    while(t[x])
    {
        next = t[x];

        t[x] = y;

        x = next;
    }

    return y;
}

#define SIZE 200000

__attribute__((always_inline)) void read(int &num)
{
    static char inBuffer[SIZE];

    static int p = ~-SIZE; num = 0x0;

    while(inBuffer[p] < 0x30 | inBuffer[p] > 0x39)
    {
        ++p != SIZE || (fread(inBuffer, 0x1, SIZE, stdin), p = 0x0);
    }

    while(inBuffer[p] > 0x2F & inBuffer[p] < 0x3A)
    {
        num = num * 0xA + inBuffer[p] - 0x30;

        ++p != SIZE || (fread(inBuffer, 0x1, SIZE, stdin), p = 0x0);
    }
}

int main()
{
    int n, m, cod, x, y;

    read(n); read(m);

    while(m--)
    {
        read(cod); read(x); read(y);

        if(cod & 1)
        {
            t[root(x)] = root(y);
        }
        else
        {
            if (root(x) ^ root(y)) fout << "NU\n";

            else fout << "DA\n";
        }
    }
}