Cod sursa(job #2491807)

Utilizator RaduNRadu Negovan RaduN Data 13 noiembrie 2019 10:13:33
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[100002], h[100002];
int finder (int x) {
    while(x!=t[x]) {
        x=t[x];
    }
    return x;
}
void type2 (int x, int y) {
    bool c=finder(x)==finder(y);
    if(c) {
        g<<"DA"<<'\n';
    } else {
        g<<"NU"<<'\n';
    }
}
void type1 (int x, int y) {
    if(h[x]==h[y]) {
        t[y]=x;
        h[x]++;
    } else if (h[x]>h[y]) {
        t[y]=x;
    } else
        t[x]=y;
}
int main() {
    int n, m, tip, x, y, tx, ty;
    f>>n>>m;
    for (int i=1; i<=n; i++) {
        t[i]=i;
        h[i]=1;
    }
    for (int i=1; i<=m; i++) {
        f>>tip>>x>>y;
        tx=finder(x);
        ty=finder(y);
        if(tip==2) {
            type2(x, y);
        } else {
            type1(tx, ty);
        }
    }
    return 0;
}