Cod sursa(job #2837333)

Utilizator 100pCiornei Stefan 100p Data 22 ianuarie 2022 09:52:51
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
#define ull unsigned long long
#define FILES freopen("disjoint.in","r",stdin);\
              freopen("disjoint.out","w",stdout);
#define CMAX 1000000
#define fastio std::ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
#define mp make_pair
#define INF 999999999999999
#define void inline void
#define mod 1000000007
#define ll long long
#define MAX 100000
#define SMAX 500000
#define pb push_back
#define add emplace_back
using namespace std;
int n,m,root[MAX+5];
int Find(int x)
{
    if(x != root[x]) return root[x] = Find(root[x]);
    else return x;
}
int main()
{
    fastio
    FILES
    cin >> n >> m;
    for(int i = 1;i <= n; ++i) root[i] = i;
    for(int i = 1;i <= m; ++i)
    {
        int a,b,c;
        cin >> a >> b >> c;
        if(a == 1)
        {
            int r = Find(b),r2 = Find(c);
            if(r > r2) root[r] = r2;
            else root[r2] = r;
        }
        else
        {
            int r = Find(b),r2 = Find(c);
            if(r == r2) cout << "DA\n";
            else cout << "NU\n";
        }
    }
}