Cod sursa(job #2950861)

Utilizator bubblegumixUdrea Robert bubblegumix Data 4 decembrie 2022 19:42:39
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.59 kb
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define lsb(x) ((x) & (-x))
#define max(...) max({__VA_ARGS__})
#define min(...) min({__VA_ARGS__})
#define hmap unordered_map
#define var auto &
#define hset unordered_set
#define pq priority_queue
#define exists(x, v) (v.find(x) != v.end())
#define inrange(x, a, b) (x >= a && x <= b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef pair<ull, ull> p64_u;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef HOME
    ~debug() { cerr << endl; }
    eni(!= ) cerr << boolalpha << i; ris;
}
eni(== ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";

}


#else
    sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define darr(x,n) debug() <<#x<<": "<< range(x,x+n+1)
#define dmat(x,n,m) for(int i=0;i<=n;i++) if(!i) debug() <<#x << ":" << range(x[i],x[i]+m+1); else debug() << range(x[i],x[i]+m+1);
const int lim = 1e5 + 5;

int n, m;
int t[lim];
int r[lim];

int find(int x)
{
    if (!t[x])
        return x;
    return t[x] = find(t[x]);
}

void unite(int x, int y)
{
    int tx = find(x);
    int ty = find(y);
    if (tx != ty)
    {
        if (r[tx] < r[ty])
            t[tx] = ty;
        else
        {
            t[ty] = tx;
            if (r[tx] == r[ty])
                r[tx]++;
        }
    }
}

int main()
{
#ifdef HOME
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    while (m--)
    {
        int op, x, y;
        cin >> op >> x >> y;
        if (op == 1)
        {
            unite(x, y);
        }
        else
        {
            cout << (find(x) == find(y) ? "DA" : "NU") << endl;
        }
    }

    return 0;
}