Cod sursa(job #1750598)

Utilizator AhileGigel Frone Ahile Data 30 august 2016 16:08:50
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 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 father[100010];


int uni (int x, int y) {

    if(father[y] == 0) {
        father[y] = x;
    } else {
        uni(x, father[y]);
    }

}

int query (int x, int y) {

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

}

int main() {

    in >> n;
    in >> m;
    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;
}