Cod sursa(job #2566382)

Utilizator NotTheBatmanBruce Wayne NotTheBatman Data 2 martie 2020 20:56:58
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <iostream>
#include <stack>

using namespace std;

const int N = 1e5 + 5;

int f[N], n;

inline void Union (int x, int y)
{
    f[x] = y;
}

int Find (int x)
{
    int root, y;
    root = x;
    while (f[root])
        root = f[root];
    while (x != root)
    {
        y = f[x];
        f[x] = root;
        x = y;
    }
    return root;
}

void Read ()
{
    ifstream fin ("disjoint.in");
    ofstream fout ("disjoint.out");
    int q;
    fin >> n >> q;
    while (q--)
    {
        int op, x, y;
        fin >> op >> x >> y;
        x = Find(x);
        y = Find(y);
        if (op == 1)
        {
            if (x != y)
                Union(x, y);
        }
        else
        {
            if (x == y) fout << "DA\n";
            else fout << "NU\n";
        }
    }
    fout.close();
    fin.close();
}



int main()
{
    Read();
    return 0;
}