Cod sursa(job #1220416)

Utilizator ptquake10ptquake10 ptquake10 Data 17 august 2014 12:36:58
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <fstream>
using namespace std;
#define inf 0xfffffff
#define MOD 1999999973

int n, m, rad[100010];
struct muchie{
	int a, b, c;
};

int tata(int x) {
	if (x == rad[x]) return x; {
		rad[x] = tata(rad[x]);
		return rad[x];
	}
}

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

	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) rad[i] = i;
	for (int i = 1; i <= m; i++) {
		scanf("%d %d %d", &op, &x, &y);
		if (op == 1) {
			rad[tata(x)] = tata(y);
		} else {
			if (tata(x) == tata(y)) 
				printf("DA\n");
			else
				printf("NU\n");
		}
	}

	
	return 0;
}