Cod sursa(job #2525980)

Utilizator alexdumitrescuDumitrescu George Alex alexdumitrescu Data 18 ianuarie 2020 09:55:27
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#define Nmax 100005

using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int t[Nmax], s[Nmax], i, j, n, m, op, x, y;
int gaseste(int x)
{
    if(t[x]!=x)
        t[x]=gaseste(t[x]);
    return t[x];
}
void reuneste(int x, int y)
{
    if(s[x]>=s[y])
        {
            t[y]=x;
            s[x]+=s[y];
        }
    else {
            t[x]=y;
            s[y]+=s[x];
    }
}
int main()
{
    fin >> n >> m;
    for(i=1;i<=n;i++)
    {
        t[i]=i;
        s[i]=1;
    }
    for(i=1;i<=m;i++)
    {
        fin >> op >> x >> y;
        gaseste(x);
        gaseste(y);
        if(op==1)
            reuneste(t[x], t[y]);
        else
        {
            if(t[x]==t[y])
                fout << "DA" << '\n';
            else fout << "NU" << '\n';
        }
    }
    return 0;
}