Cod sursa(job #2681364)

Utilizator unicornrozmiruna protopopescu unicornroz Data 5 decembrie 2020 12:08:05
Problema Paduri de multimi disjuncte Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int t[100000], r[100000];

void u(int x, int y) {
    if (r[x]+1>r[y]+1) {
        t[y]=x;
    } else if (r[x]+1<r[y]+1) {
        t[x]=y;
    } else {
        t[y]=x;
        r[x]++;
    }
}

int f(int x) {
    while (t[x]!=0) x=t[x];
    return x;
}

int main() {
    int N, M, cod, x, y, i, tx, ty;
    in >> N >> M;
    for (i=1; i<=N; i++)
    for (i=0; i<M; i++) {
        in >> cod >> x >> y;
        if (cod==1) {
            tx=f(x);
            ty=f(y);
            u(tx, ty);
        } else {
            tx=f(x);
            ty=f(y);
            if (tx==ty) out << "DA\n";
            else out << "NU\n";
        }
    }
    return 0;
}