Cod sursa(job #2691870)

Utilizator tomaionutIDorando tomaionut Data 30 decembrie 2020 13:23:23
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[100005],n,m;
int Find(int x)
{
    int r,y;
    r=x;
    while (t[r]!=0)
        r=t[r];
    while (x!=r)
    {
        y=t[x];
        t[x]=r;
        x=y;
    }
    return r;
}
void Union(int x, int y)
{
    t[y]=x;
}
int main()
{
    int i,op,x,y;
    fin >> n >> m;
    for (i=1; i<=m; i++)
    {
        fin >> op >> x >> y;
        if (op==1)
        {
            x=Find(x);
            y=Find(y);
            if (x!=y) Union(x,y);
        }
        else if (op==2)
        {
            x=Find(x);
            y=Find(y);
            if (x==y) fout << "DA\n";
            else fout << "NU\n";
        }
    }

    return 0;
}