Cod sursa(job #3000155)

Utilizator DemonulCristian Razvan Gavrilescu Demonul Data 12 martie 2023 00:17:01
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int t[1000002];
int cerinta[100002];
int rad(int x)
{
    while(t[x]!=x)
    {
        t[x]=t[t[x]];
        x=t[x];
    }

    return x;
}
int main()
{
    int n , m;
    int j = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; ++i){
        t[i] = i;
    }
    while(m){

        m--;
       int tip , x , y;
       cin >> tip >> x >> y;
       int a = rad(x);
       int b = rad(y);
      if(tip == 2){
       if(a == b){
         cerinta[++j] = 1;
       }
       else{
        cerinta[++j] = 0;
       }
      }
      if(tip == 1){
         t[a] = b;
      }

    }
    for(int i = 1; i <= j; ++i){
        if(cerinta[i] == 1){
            cout <<"DA" <<'\n';
        }
        else{
            cout <<"NU" <<'\n';
        }
    }

    return 0;
}