Cod sursa(job #1808860)

Utilizator calinfloreaCalin Florea calinflorea Data 18 noiembrie 2016 11:48:12
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[100001],n,m,cod,x,y;
int Find(int x)
{
    int rad,y;
    rad = x;
    while(t[rad] != 0)
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
}
inline void Union(int  x,int y)
{
    t[x] = y;
}
void Citire()
{
    int i;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> cod >> x >> y;
        x = Find(x);
        y = Find(y);
        if(cod == 1 && x != y)
            Union(x,y);
        if(cod == 2)
            if(x == y) fout << "DA" << "\n";
            else fout << "NU" << "\n";
    }
}
int main()
{
    Citire();
    return 0;
}