Cod sursa(job #2304030)

Utilizator AlexPascu007Pascu Ionut Alexandru AlexPascu007 Data 17 decembrie 2018 14:01:10
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m,t[100010],cod,x,y,rx,ry,i;
int rad(int nod) {
    while (t[nod]>0)
        nod=t[nod];
    return nod;
}
int main() {
    fin>>n>>m;
    for (i=1;i<=n;i++)
        t[i]=-1;
    for (i=1;i<=m;i++) {
        fin>>cod>>x>>y;
        rx=rad(x); ry=rad(y);
        if (cod==1) {
            if (rx!=ry) {
                if (t[rx]<t[ry]) {
                    t[rx]+=t[ry];
                    t[ry]=rx;
                }
                else {
                    t[ry]+=t[rx];
                    t[rx]=ry;
                }
            }
        }
        else
            fout<<((rx==ry)?"DA\n":"NU\n");
    }
    return 0;
}