Cod sursa(job #2832333)

Utilizator DMR6476Erdic Dragos DMR6476 Data 13 ianuarie 2022 14:31:36
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include<fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int theRoots[1000005];
int main()
{
    int n,m;
    fin>>n>>m;
    for(int i=1; i<=n; i++)
        theRoots[i] = i;
    for(int i=0; i<m; i++)
    {
        int t,x,y;
        fin>>t>>x>>y;
        if(t == 1)
        {
            if(theRoots[x] != theRoots[y])
            {
                for(int j=1; j<=n; j++)
                    if(theRoots[j] == theRoots[y])
                        theRoots[j] = theRoots[x];
            }
        }
        else
        {
            if(theRoots[x] != theRoots[y])
                fout<<"NU"<<'\n';
            else
                fout<<"DA"<<'\n';
        }
    }
    fin.close();
    fout.close();
    return 0;
}