Cod sursa(job #2278784)

Utilizator Teodor01Dorde Teodor Teodor01 Data 8 noiembrie 2018 15:56:45
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>

using namespace std;
int T[100001];
ifstream in ("disjoint.in");
ofstream out("disjoint.out");
int sef(int x){
    if(x!=T[x])
        T[x]=sef(T[x]);
    return T[x];
}
void join(int x,int y){
    int t1=sef(x),t2=sef(y);
    T[t2]=t1;
}
bool q1(int x,int y){
    int t1=sef(x),t2=sef(y);
    if(t1==t2)
        return 1;
    return 0;
}
int main()
{
    int n,m,i,c,x,y;
    in>>n>>m;
    for(i=1;i<=n;i++)
        T[i]=i;
    for(i=1;i<=m;i++){
        in>>c>>x>>y;
        if(c==1)
            join(x,y);
        else
            if(q1(x,y)==1)
                out<<"DA\n";
            else
                out<<"NU\n";
    }
    return 0;
}