Cod sursa(job #1910702)

Utilizator BlackNestaAndrei Manaila BlackNesta Data 7 martie 2017 17:56:55
Problema Aria Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>
#define Nmax 100005

using namespace std;

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

int N, t[Nmax];

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

inline int Find(int x)
{
    int rad, aux;
    rad = x;
    while(t[rad])
        rad = t[rad];
    while(x != rad)
    {
        aux = t[x];
        t[x] = rad;
        x = aux;
    }
    return rad;
}

inline void Solve()
{
    int M, i, x, y, op;
    fin >> N >> M;
    for(i = 1; i <= M; i++)
    {
        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";
        }
    }
}

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