Cod sursa(job #2576387)

Utilizator natrovanCeval Marius natrovan Data 6 martie 2020 18:59:24
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <map>
#define NMAX 100005

using namespace std;

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

int papa[NMAX],n,x,y,t;
int nume[NMAX],nrtag=0;
map <int,int> c;

int root(int x){
    if(papa[x] == x)
        return x;
    papa[x] = root(papa[x]);
    return papa[x];
}

int main()
{
    fin>>n;
    for(int i = 1; i <= n; i++){
        fin >> x >> y >> t;
        if(c.find(x) == c.end()){
            nrtag++;
            c[x] = nrtag;
            papa[nrtag] = nrtag;
        }

        if(c.find(y) == c.end()){
            nrtag++;
            c[y] = nrtag;
            papa[nrtag] = nrtag;
        }

        if(t == 2){
            papa[root(c[x])] = root(c[y]);
        } else{
            if(root(c[x]) == root(c[y])){
                fout << "DA";
            } else{
                fout<<"NU";
            }
        }

    }

    return 0;
}