Cod sursa(job #3001085)

Utilizator alexcostinCostin Alexandru alexcostin Data 13 martie 2023 11:01:14
Problema Paduri de multimi disjuncte Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NMax = 500;
int T[NMax+1];

int Find (int i) {
    if (T[i] == 0)
        return i;
    return T[i] = Find(T[i]);
}

inline void Union(int cx, int cy) {
    T[cy] = cx;
}

int main()
{
    int N, M, op, x, y, cx, cy;
    f >> N >> M;
    while (M--) {
        f >> op >> x >> y;
        cx = Find(x);
        cy = Find(y);
        if (op == 1) {
            if (cx != cy) {
                Union(cx, cy);
            }
        }
        else {
            if (cx == cy)
                g << "DA\n";
            else
                g << "NU\n";
        }
    }
    return 0;
}