Cod sursa(job #765648)

Utilizator igsifvevc avb igsi Data 8 iulie 2012 16:40:53
Problema Paduri de multimi disjuncte Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.87 kb
#include<stdio.h>

int set[100001], choose[100001], op, x, y, N, M, setX, setY;
FILE *in, *out;

int main()
{
    in = fopen("disjoint.in", "r");
    out = fopen("disjoint.out", "w");

    fscanf(in, "%d %d", &N, &M);

    for(x = 1; x <= N; x++) set[x] = x;

    for(; M; --M)
    {
        fscanf(in, "%d %d %d", &op, &x, &y);
        for(; set[x] != x; x = set[x]);
        for(; set[y] != y; y = set[y]);

        switch(op)
        {
            case 1:
                if(choose[x] >= choose[y])
                    set[y] = set[x],
                    choose[x]++;
                else
                    set[x] = set[y],
                    choose[y]++;
                break;
            case 2:
                if(x == y) fprintf(out, "DA\n");
                else fprintf(out, "NU\n");

                break;
        }
    }

    fclose(in);
    fclose(out);
    return 0;
}