Cod sursa(job #3004124)

Utilizator andreea0146Nicula Andreea andreea0146 Data 16 martie 2023 10:06:24
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include<bits/stdc++.h>
using namespace std;
const int nmax=100000;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[nmax+1];
int n;
int Find(int i)
{
    if(t[i]==0) return i;
    return t[i]=Find(t[i]);
}
inline void Union(int cx,int cy)
{
   t[cy]=cx;
}
int main()
{
    int m,op,x,y,cx,cy;
    f>>n>>m;
    while(m--)
    {
        f>>op>>x>>y;
        cx=Find(x);
        cy=Find(y);
        if(op==1)
        {
            if(cx!=cy) Union(cx,cy);
        }
        else
        {
            if(cx==cy) g<<"DA\n";
            else g<<"NU\n";
        }
    }
}