Cod sursa(job #2237167)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 31 august 2018 20:25:42
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef pair <int, int> pii;
typedef vector <int> vi;
typedef long long ll;
typedef unsigned long long ull;

ifstream f ("disjoint.in");
ofstream g ("disjoint.out");

const int NMAX = 1e5 +5;
int dad[NMAX];
int n, m;

int getDad (int x) {
    if (dad[x] == x) return x;
    dad[x] = getDad (dad[x]);
    return dad[x];
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL_DEFINE
	freopen (".in", "r", stdin);
#endif
    f >> n >> m;
    for (int i = 1; i <= n; ++i) dad[i] = i;
    for (int i = 0; i < m; ++i) {
        int cod, x, y;
        f >> cod >> x >> y;
        int dad1 = getDad (x);
        int dad2 = getDad (y);
        if (cod == 1) {
            dad[dad2] = dad1;
        } else {
            g << ((dad[x] == dad[y]) ? ("DA\n") : ("NU\n"));
        }
    }

    f.close();
    g.close();
    return 0;
}