Cod sursa(job #2977268)

Utilizator TheChiChiriac Theodor TheChi Data 11 februarie 2023 10:33:35
Problema Paduri de multimi disjuncte Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
using namespace std;

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

int g[100002];
int main()
{
    int N,M;
    fin>>N;
    for(int i=1;i<=N;i++) g[i] = i;
    int t, x, y;
    fin>>M;
    for(int i=1;i<=M;i++){
        fin>>t>>x>>y;
        if(t==1){
            int xx = g[x];
            int yy = g[y];

            for(int j=1;j<=N;j++){
                if(g[j] == yy) g[j] = xx;
            }
        }
        else (g[x] == g[y]) ? fout<<"DA\n":fout<<"NU\n";
    }

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