Cod sursa(job #2871091)

Utilizator LaserDenisCrismariu Denis LaserDenis Data 12 martie 2022 20:56:35
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.16 kb
#include <bits/stdc++.h>

using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int a, int b)
{
    int ans = uniform_int_distribution<int>(a, b)(rng);
    return ans;
}
#define fastio std::ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define test " test "
#define yes "YES"
#define no "NO"
#define bn '\n'
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
//#define int long long
#define db(a) cout << #a << "=" << (a) << " ";
#define deb(a) db(a) cout << '\n';
#define deb2(a, b) db(a) db(b) cout << '\n';
#define deb3(a, b, c) db(a) db(b) db(c) cout << '\n';
#define deb4(a, b, c, d) db(a) db(b) db(c) db(d) cout << '\n';
#define deb5(a, b, c, d, e) db(a) db(b) db(c) db(d) db(e) cout << '\n';
#define readV(v, n)             \
    for (int i = 0; i < n; i++) \
        cin >> v[i];
#define printV(v)    \
    for (auto i : v) \
        cout << i << " ";
#define FILES                           \
    freopen("disjoint.in", "r", stdin); \
    freopen("disjoint.out", "w", stdout);
const int mod = 1e9 + 7;
int maxi = INT_MIN, mini = INT_MAX;
int n, q;
int c, x, y;
const int MAX = 100005;
int TT[MAX], RG[MAX];
int find(int x)
{
    int root = x;
    while (TT[root] != root)
        root = TT[root];
    while (TT[x] != x)
    {
        int y = TT[x];
        TT[x] = root;
        x = y;
    }
    return root;
}
void unite(int x, int y)
{
    if (RG[x] > RG[y])
        TT[y] = x;
    else
        TT[x] = y;

    if (RG[x] == RG[y])
        RG[y]++;
}
void solve()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        TT[i] = i, RG[i] = 1;
    while (q--)
    {
        cin >> c;
        cin >> x >> y;
        switch (c)
        {
        case 1:
            unite(find(x), find(y));
            break;

        default:
            if (find(x) == find(y))
                cout << "DA\n";
            else
                cout << "NU\n";
            break;
        }
    }
}
signed main()
{
    fastio;
     FILES;
    int __t = 1;
    // cin >> __t;
    while (__t--)
        solve();

    return 0;
}