Cod sursa(job #2162159)

Utilizator CraiuAndrei Craiu Craiu Data 12 martie 2018 08:26:51
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nMax = 100005;
int N, M;
int t[nMax];

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

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

inline void Solve()
{
    int op, x, y;
    fin >> N >> M;
    for(int i = 1; i <= M; i++)
    {
        fin >> op >> x >> y;
        x = Find(x);
        y = Find(y);
        if(op == 1)
            Union(x, y);
        else if(x == y)
            fout << "DA\n";
        else fout << "NU\n";
    }
}

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