Cod sursa(job #1750822)

Utilizator AhileGigel Frone Ahile Data 31 august 2016 10:00:06
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include<bits/stdc++.h>
using namespace std;
#define in f
#define out g


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


int n;
int m;
int x;
int y;
int code;
int aux;
int father[100010];
int rang[100010];

int fin(int x) {

    if(father[x] == x) {
        return x;
    } else {
        rang[father[y]] = rang[y] + 1;
        return fin(father[x]);
    }

}

int uni (int x, int y) {

    int finy;
    int finx;
    finy = fin(y);
    finx = fin(x);
    if(rang[x] < rang[y]) {
        finy = aux;
        finy = finx;
        finx = finy;
    }
    father[finy] = finx;
    rang[finy]++;

}

int query (int x, int y) {

    if((father[x] == x)&&(father[y] == y)) {
        if(x == y) {
            out << "DA" << endl;
        } else {
            out << "NU" << endl;
        }
    } else {
        if(father[y] != 0) {
            query(x ,father[y]);
        } else {
            query(father[x], y);
        }
    }
}

int main() {

    in >> n;
    in >> m;
    for(int i = 1; i <= n; i++) {
        father[i] = i;
    }
    for(int i = 1; i <= m; i++) {
        in >> code;
        in >> x;
        in >> y;
        if(code == 1) {
            uni(x, y);
        }
        if(code == 2) {
            query(x, y);
        }
    }
    return 0;
}
/*
int find(int x)
{
    if(father[x] == x)
    {
        return x;
    }
    else
    {
        return find(father[x]);
    }
}

void uniune(int a, int b)
{
    int rootX = find(a);
    int rootY = find(b);

    if(rootX == rootY)
        return;
    if(ranc[rootX] > ranc[rootY])
        father[rootY] = rootX;
    else if(ranc[rootX] < ranc[rootY])
        father[rootX] = rootY;
    else
    father[rootX] = rootY;
    ranc[rootY]++;

}
*/