Cod sursa(job #2064617)

Utilizator calin9819Costea Calin calin9819 Data 12 noiembrie 2017 16:41:43
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <fstream>
using namespace std;

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

struct nod {
    nod *t;
    int v;
} noduri[100001];

int main()
{
    int N, M;
    f >> N >> M;
    for (int i = 1; i <= N; i++) {
        noduri[i].v = i;
        noduri[i].t = NULL;
    }

    nod *c, *d;
    for (int i = 1; i <= M; i++) {
        int q, x, y;
        f >> q >> x >> y;
        if (q == 1) {
            if (noduri[x].t != NULL) {
                c = noduri[x].t;
                while (c->t != NULL)
                    c = c->t;
            }
            if (noduri[y].t != NULL) {
                d= noduri[y].t;
                while (d->t != NULL)
                    d = d->t;
            }
            if (noduri[x].t == NULL && noduri[y].t == NULL)
                    noduri[y].t = noduri+x;
            else if (c->v != d->v)
                d->t = c;

        } else {
            int t1, t2;
            if (noduri[x].t != NULL) {
                c = noduri[x].t;
                while (c->t != NULL)
                    c = c->t;
                t1 = c->v;
            }
            else t1 = noduri[x].v;
            if (noduri[y].t != NULL) {
                c = noduri[y].t;
                while (c->t != NULL)
                    c = c->t;
                t2 = c->v;
            }
            else t2 = noduri[y].v;
            if (t1 == t2) g << "DA\n";
            else g << "NU\n";
        }
    }
    return 0;
}