Cod sursa(job #3197527)

Utilizator matei__bBenchea Matei matei__b Data 27 ianuarie 2024 09:23:14
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 20'011
#define dim 100005
#define lim 1000000
#define INF 1000000000
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define piii pair<int,pair<int,int> > 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;
 
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n,m;
int t[dim];

int root(int nod)
{
    if(nod==t[nod])
        return nod;

    return t[nod]=root(t[nod]);
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr); 

    fin >> n >> m;

    for(int i=1; i<=n; i++)
        t[i]=i;

    while(m--)
    {
        int tip,x,y;

        fin >> tip >> x >> y;

        if(tip==1)
        {
            int rx=root(x);
            int ry=root(y);

            t[ry]=rx;
        }
        else 
        {
            int rx=root(x);
            int ry=root(y);

            if(rx==ry)
                fout << "DA\n";
            else 
                fout << "NU\n";
        }
    }

    return 0;
}