Cod sursa(job #2297542)

Utilizator AndreiLunguLungu Andrei Sebastian AndreiLungu Data 5 decembrie 2018 23:16:17
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n , m , t[100004];
void Union(int x , int y)
{
    t[y] = x;
}
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;
    }
    return rad;
}
int main()
{
    int x , y , i , op;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> op >> x >> y;
        x = Find(x);
        y = Find(y);
        if(op == 1)
        {
            if(x != y)Union(x , y);
        }
        else
        {
            if(x == y)fout << "DA\n";
            else fout << "NU\n";
        }
    }
    return 0;
}