Cod sursa(job #1243645)

Utilizator Cezar_MihalceaCezar Mihalcea Cezar_Mihalcea Data 16 octombrie 2014 09:39:38
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include<fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[1000001];
int rad(int x)
{
    if(t[x]==0)
        return x;
    t[x]=rad(t[x]);
    return t[x];
}
void unionf(int x,int y)
{
    int rx,ry;
    rx=rad(x);
    ry=rad(y);
    t[rx]=ry;
}
bool findf(int x,int y)
{
    if(rad(x)==rad(y))
        return true;
    return false;
}
int main()
{
    int n,m,i,x,y,ins;
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>ins>>x>>y;
        if(ins==1)
            unionf(x,y);
        else
            if(findf(x,y)==true)
                g<<"DA\n";
            else
                g<<"NU\n";
    }
}