Cod sursa(job #2701340)

Utilizator H7GhosTSuruceanu Daniel H7GhosT Data 30 ianuarie 2021 15:35:59
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
using namespace std;

const int mxN = 1e5 + 100;
int parent[mxN];

char buff[mxN * 3];
char * buffp = buff;

#define FL(nm) do { freopen(nm ".in", "r", stdin); freopen(nm ".out", "w", stdout); } while (false)

// 1 2 3 4 5 6 7 8
//
// 1 2 1 4 5 6 7 8
// 1 2 1 4 5 6 5 8

int getRoot(int i) {
    int pi = i;
    while (parent[pi] != pi) pi = parent[pi];
    while (parent[i] != i) {
        int tmp = parent[i];
        parent[i] = pi;
        i = tmp;
    }
    return pi;
}
int main() {
    FL("disjoint");
    int n, m;

    scanf("%d%d", &n, &m);

    for (int i = 0; i < n + 1; i++) {
        parent[i] = i;
    }

    while (m--) {
        int o, i, j;
        scanf("%d%d%d", &o, &i, &j);
        i = getRoot(i);
        j = getRoot(j);

        if (o == 1) {
            parent[j] = parent[i];
        } else if (o == 2) {
            if (i == j) {
                strcpy(buffp, "DA\n");
                // puts("DA");
            } else {
                strcpy(buffp, "NU\n");
                // puts("NU");
            }
            buffp += 3;
        }
    }

    puts(buff);

    return 0;
}