Cod sursa(job #1995792)

Utilizator calinfloreaCalin Florea calinflorea Data 29 iunie 2017 09:21:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int t[100005], x, y, cod;
int n, m;
int Find(int x)
{
    int y, rad;

    rad = x;

    while(t[rad])
        rad = t[rad];

    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

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 << "NU\n";
            else fout << "DA\n";
        }
    }
}
int main()
{
    Citire();
    return 0;
}