Cod sursa(job #2561084)

Utilizator matthriscuMatt . matthriscu Data 28 februarie 2020 16:22:22
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <cstdio>
using namespace std;

int n, m, v[100005], i, q, x, y, j, t;

int main() {
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(i = 1; i <= n; ++i)
        v[i] = i;
    for(i = 1; i <= m; ++i) {
        scanf("%d%d%d", &q, &x, &y);
        if(q == 1) {
            t = v[y];
            for(j = 1; j <= n; ++j) {
                if(v[j] == t)
                    v[j] = v[x];
            }
            /*for(j = 1; j <= n; ++j)
                printf("%d ", *(v+j));
            printf("\n");*/
        }
        else
            if(v[x] == v[y])
                printf("DA\n");
            else
                printf("NU\n");
    }
}