Cod sursa(job #2637220)

Utilizator NeganAlex Mihalcea Negan Data 21 iulie 2020 22:05:59
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

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

const int NMAX = 100005;
int n, m;
int father[NMAX];
int Union(int x, int y)
{
    father[y] = x;
}
int Find(int x)
{
    int r = x;
    while(father[r] != 0)
    {
        r = father[r];
    }
    while(x != r)
    {
        int aux = father[x];
        father[x] = r;
        x = aux;
    }
    return r;
}

int main()
{
    int i, op, x, y;
    fin >> n >> m;
    for(i = 1;i <= m;i++)
    {
        fin >> op >> x >> y;
        x = Find(x);
        y = Find(y);
        if(op == 1 && x != y)
            Union(x, y);
        else if(op == 2 && x == y)
            fout << "DA\n";
        else if(op == 2 && x != y)
            fout << "NU\n";
    }
    return 0;
}