Cod sursa(job #1632279)

Utilizator tothalToth Alexandru tothal Data 5 martie 2016 23:47:20
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m;
const int Nmax=100005;
int TT[Nmax],RG[Nmax];

void unite(int x,int y)
{
    if(RG[x]>RG[y])
        TT[y]=x;
    if(RG[x]<RG[y])
    TT[x]=y;
    if(RG[x]==RG[y])
    {
        TT[x]=y;
        RG[y]++;
    }
}
int gas(int x)
{
    while(x!=TT[x])
        x=TT[x];

    return x;
}


int main()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        TT[i]=i;
        RG[i]=1;
    }
    int x,y,cod;
    for(int i=1;i<=m;i++)
    {fin>>cod>>x>>y;
    if(cod==1)
        unite(TT[x],TT[y]);
    else
        if(gas(TT[x]) == gas(TT[y]))
            fout<<"DA\n";
        else
            fout<<"NU\n";
    }
}