Cod sursa(job #983816)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 12 august 2013 19:41:59
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int N,M,TT[100002];
int Father(int x)
{
    while(TT[x]!=x)
        x=TT[x];
    while(TT[x]!=x)
        {
        int y=TT[x];
        TT[x]=x;
        x=y;
        }
    return x;
}
void Unite(int x,int y)
{
    TT[x]=y;
}
void Solve()
{
    int i;
    f>>N>>M;
    for(i=1;i<=N;i++)
        TT[i]=i;
    for(i=1;i<=M;i++)
    {
        int cod,x,y;
        f>>cod>>x>>y;
        if(cod==1)
            Unite(Father(x),Father(y));
        else
        {
            if(Father(x)==Father(y))
                g<<"DA\n";
            else
                g<<"NU\n";
        }
    }
}
int main()
{
    Solve();
    return 0;
}