Cod sursa(job #1182086)

Utilizator paulhermanPaul Herman paulherman Data 4 mai 2014 18:21:17
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <cstdio>

const int MAX_SETS = 100005;

int set_search(int set_parent[MAX_SETS], int e) {
	while (set_parent[e] != e)
		e = set_parent[e];
	return e;
}

void set_union(int set_parent[MAX_SETS], int set_height[MAX_SETS], int x, int y) {
	int set_x = set_search(set_parent, x);
	int set_y = set_search(set_parent, y);
	if (set_height[set_x] > set_height[set_y])
		set_parent[set_y] = set_x;
	else if (set_height[set_x] < set_height[set_y])
		set_parent[set_x] = set_y;
	else {
		set_parent[set_y] = set_x;
		set_height[set_x] += 1;
	}
}

void set_init(int set_parent[MAX_SETS], int set_height[MAX_SETS], int set_count) {
	for (int i = 1; i <= set_count; i++) {
		set_parent[i] = i;
		set_height[i] = 1;
	}
}

int main() {
	freopen("disjoint.in", "r", stdin);
	freopen("disjoint.out", "w", stdout);

	int set_count, query_count, set_parent[MAX_SETS], set_height[MAX_SETS];

	scanf("%d %d", &set_count, &query_count);
	set_init(set_parent, set_height, set_count);

	for (int i = 0; i < query_count; i++) {
		int query_type, x, y;
		scanf("%d %d %d", &query_type, &x, &y);
		if (query_type == 1) // union
			set_union(set_parent, set_height, x, y);
		else { // same set?
			if (set_search(set_parent, x) == set_search(set_parent, y))
				printf("DA\n");
			else
				printf("NU\n");
		}
	}

	return 0;
}