Cod sursa(job #3268017)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 13 ianuarie 2025 15:25:58
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("disjoint.in");
ofstream g ("disjoint.out");

const int NMAX = 1e5;

int tata[NMAX+1], n;

int sef(int x){
    
    if(x == tata[x])
        return x;
        
    return tata[x] = sef(tata[x]);
    
}

void uneste(int x, int y){
    
    tata[sef(x)] = sef(y);
    
}

int main()
{
    
    int q;
    f >> n >> q;
    
    for(int i=1; i<=n; i++)
        tata[i] = i;
        
    for(int i=1; i<=q; i++){
        
        int t, x, y;
        f >> t >> x >> y;
        
        if(t == 1){
            
            uneste(x, y);
            
        }else{
            
            if(sef(x) == sef(y))
                g << "DA" << "\n";
                
            else g << "NU" << "\n";
            
        }
        
        
    }
    
    

    return 0;
}