Cod sursa(job #2942680)

Utilizator simonatudoroiuTudoroiu Simona simonatudoroiu Data 19 noiembrie 2022 22:44:05
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int main()
{
    int n,m,a,b,c;
    vector<vector<int>> list;
    vector<int> tata;
    fin>>n>>m;
    for(int i=0;i<=n;i++)
    {
        tata.push_back(i);
        list.push_back({});
    }
    for(int x=1;x<=m;x++)
    {
        fin>>a>>b>>c;
        if(a == 1)
        {
            if(list[c].size() < list[b].size()) {
                tata[c] = tata[b];
                list[b].push_back(c);
                for (int i: list[c]) {
                    tata[i] = tata[b];
                    list[b].push_back(i);
                }
                list[c].clear();
            }
            else
            {
                tata[b] = tata[c];
                list[c].push_back(b);
                for (int i: list[b]) {
                    tata[i] = tata[c];
                    list[c].push_back(i);
                }
                list[b].clear();
            }
        }
        else
        {
            if(tata[b] == tata[c])
                fout<<"DA\n";
            else
                fout<<"NU\n";
        }

    }
    fin.close();
    fout.close();
    return 0;
}