Cod sursa(job #877924)

Utilizator Impaler_009Mihai Nitu Impaler_009 Data 13 februarie 2013 14:27:00
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m,v[100001];

void reunion (int x,int y)
{
    int mx=x,my=y,temp;
    while (v[mx]!=0) mx=v[mx];
    while (my!=0)
    {
        temp=v[my];
        v[my]=mx;
        my=temp;
    }
}

void discern (int x,int y)
{
    while (v[x]!=0) x=v[x];
    while (v[y]!=0) y=v[y];
    if (x==y) fout<<"DA";
    else fout<<"NU";
    fout<<"\n";
}

int main()
{
    int o,x,y,i;
    fin>>n>>m;
    for (i=1;i<=m;i++)
    {
        fin>>o>>x>>y;
        if(o==1) reunion (x,y);
        else discern (x,y);
    }
}