Cod sursa(job #2731780)

Utilizator emanuel2186Lugojan Emanuel emanuel2186 Data 28 martie 2021 11:54:47
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N, M;
int tata[Nmax];
int rang[Nmax];
int father(int nod)
{
    while(tata[nod] != nod)
        nod = tata[nod];
    return nod;
}
void unire(int x, int y)
{
    if(rang[x] < rang[y])
        tata[x] = y;
    if(rang[y] < rang[x])
        tata[y] = x;
    if(rang[x] == rang[y])
    {
        tata[x] = y;
        rang[y]++;
    }
}
void initializare()
{
    for(int i=1; i<=N; i++)
    {
        tata[i] = i;
        rang[i] = 1;
    }
}
void citire()
{
    fin>>N>>M;
    int cer, a, b;
    int tatal_x, tatal_y;
    initializare();
    for(int i=1; i<=M; i++)
    {
        fin>>cer>>a>>b;
        tatal_x = father(a);
        tatal_y = father(b);
        if(cer == 1)
        {
            if(tatal_x != tatal_y)
            {
                unire(tatal_x, tatal_y);
            }
        }
        else
        {
            if(tatal_x == tatal_y)
                fout<<"DA";
            else
                fout<<"NU";
            fout<<"\n";
        }
    }
}
int main()
{
    citire();
    return 0;
}