Cod sursa(job #3316048)

Utilizator CheeseEaterHackRoman Alex CheeseEaterHack Data 17 octombrie 2025 08:47:27
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n, k, tata[100001], cer, x, y;

int comp(int x)
{
  int y=x;
   while (x!=tata[x])
   {
     x=tata[x];
   }
   while (y!=x)
   {
     int a=tata[y];
     tata[y]=x;
     y=a;
   }
   return tata[y];
}

void unify(int x, int y)
{
  int x1=comp(x);
  int y1=comp(y);
  tata[x1]=y1;
}

int main()
{
    fin>>n>>k;
    for (int i=1; i<=n; i++)
    {
      tata[i]=i;
    }
    for (int z=1; z<=k; z++)
    {
      fin>>cer>>x>>y;
      if (cer==1)
      {
        unify(x, y);
      }
      else
      {
        if (comp(x)==comp(y))
        {
          fout<<"DA"<<'\n';
        }
        else
        {
          fout<<"NU"<<'\n';
        }
      }
    }







    return 0;
}