Cod sursa(job #3029973)

Utilizator CalinHanguCalinHangu CalinHangu Data 17 martie 2023 12:30:29
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

ifstream in("f.in");
ofstream out("f.out");

const int NMAX = 1e5 + 5;
const char nl = '\n';

int n, m, father[NMAX];

int root(int x){
    if(x == father[x])
        return x;
    return father[x] = root(father[x]);
}

void unite(int x, int y){
    father[root(x)] = root(y);
}

int main()
{
    in >> n >> m;
    for(int i = 1; i <= n; ++i)
        father[i] = i;
    for(int i = 1; i <= m; ++i){
        int q, x, y;
        in >> q >> x >> y;
        if(q == 1){
            if(root(x) != root(y))
                unite(x, y);
        }
        else{
            if(root(x) != root(y))
                out << "NU" << nl;
            else
                out << "DA" << nl;
        }
    }
    return 0;
}