Cod sursa(job #1907265)

Utilizator medicinedoctoralexandru medicinedoctor Data 6 martie 2017 18:32:39
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <vector>
//#include <iostream>

using namespace std;

ifstream cin ("disjoint.in" );
ofstream cout("disjoint.out");

int s = 0;
vector <int> a;

int main()
{
    int n, m, x, y, z;
    cin >> n >> m;
    a.resize(n + 1);
    for (int i = 0; i < a.size(); i++)
        a[i] = i;

    for (int i = 0; i < m; i++)
    {
        cin >> z >> x >> y;

        if (z == 1)
        {//unirea
            int q;
            while (a[x] != x)
            {
                q = x;
                x = a[x];
                a[q] = y;
            }
            a[x] = y;
        }
        else
        {//afisarea
            while (a[x] != x)
                x = a[x];
            while (a[y] != y)
                y = a[y];
            if (x == y) cout << "DA\n";
            else cout << "NU\n";
        }
    }

    return 0;
}