Cod sursa(job #2645264)

Utilizator ZahaZaharie Stefan Zaha Data 27 august 2020 16:42:17
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>
#include <set>
using namespace std;

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

set<int> neighbours[100005];

int main() {
    int n, m;
    fin >> n >> m;

    for (int i = 1; i <= m; ++i) {
        int type, x, y;
        fin >> type >> x >> y;

        if (type == 1) {
            set<int> toMove;

            for (int i : neighbours[x])
                toMove.emplace(i);
            for (int i : neighbours[y])
                toMove.emplace(i);

            toMove.emplace(y);
            toMove.emplace(x);

            auto it1 = toMove.begin();
            while (it1 != toMove.end()) {
                auto it2 = it1;
                ++it2;

                while (it2 != toMove.end()) {
                    neighbours[*it1].emplace(*it2);
                    neighbours[*it2].emplace(*it1);

                    ++it2;
                }
                ++it1;
            }
        }
        else {
            auto found = neighbours[x].find(y);

            if (found != neighbours[x].end())
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }

    return 0;
}