Cod sursa(job #1054363)

Utilizator Athena99Anghel Anca Athena99 Data 13 decembrie 2013 19:37:40
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>

using namespace std;

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

const int nmax= 100000;

int v[nmax+1];

int up( int a, int b ) {
    if ( v[a]!=v[v[a]] ) {
        v[a]= up( v[a], b );
    } else if ( b>0 ) {
        v[a]= b;
    }
    return v[a];
}

int main(  ) {
    int n, m;
    fin>>n>>m;
    for ( int i= 1; i<=n; ++i ) {
        v[i]= i;
    }
    for ( ; m>0; --m ) {
        int t, x, y;
        fin>>t>>x>>y;
        if ( v[x]!=v[v[x]] ) {
            v[x]= up(x, 0);
        }
        if ( v[y]!=v[v[y]] ) {
            v[y]= up(y, 0);
        }

        if ( t==1 ) {
            v[y]= up(v[y], v[x]);
        } else {
            if ( v[x]==v[y] ) {
                fout<<"DA\n";
            } else {
                fout<<"NU\n";
            }
        }
    }

    return 0;
}