Cod sursa(job #2262725)

Utilizator tigeraOprea Tereza Emilia tigera Data 17 octombrie 2018 19:26:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
//
//  main.cpp
//  paduri_de_multimi_disjuncte
//
//  Created by Tereza Oprea on 17/10/2018.
//  Copyright © 2018 Tereza Oprea. All rights reserved.
//

#include <fstream>
#include <iostream>

using namespace std;

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

int m, n, x, y, c, p[100010];

int gaseste_rep (int nod){
    if (p[nod] == nod)
        return nod;
    p[nod] = gaseste_rep(p[nod]);
    return p[nod];
}


int main() {
    fin >> n >> m;
    for (int i=1; i<=n; i++)
        p[i] = i;
    for (int i=1; i<=m; i++)
    {
        fin >> c >> x >> y;
        if (c==1)
        {
            gaseste_rep(x);
            gaseste_rep(y);
            p[p[x]] = p[y];
        }
        else
        {
            gaseste_rep(x);
            gaseste_rep(y);
            if (p[x] == p[y])
                fout << "DA" << '\n';
            else
                fout << "NU" << '\n';
        }
    }
    return 0;
}