Cod sursa(job #1412864)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 1 aprilie 2015 16:51:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>

using namespace std;

const int maxn = 100005;

int n, m, father[maxn], sz[maxn];

inline int find(int x) {
	while(x != father[x])
		x = father[x];
	return x;
}

inline void unite(int x, int y) {
	int tx = find(x);
	int ty = find(y);
	if(tx == ty)
		return ;
	if(sz[tx] == sz[ty])
		++ sz[tx];
	if(sz[tx] > sz[ty])
		father[ty] = tx;
	else
		father[tx] = ty;
}	

int main() {
	ifstream fin("disjoint.in");
	ofstream fout("disjoint.out");

	fin >> n >> m;
	for(int i = 1 ; i <= n ; ++ i) {
		father[i] = i;
		sz[i] = 1;
	}
	for(int i = 1 ; i <= m ; ++ i) {
		int op, x, y;
		fin  >> op >> x >> y;
		if(op == 1)
			unite(x, y);
		else
			if(find(x) == find(y))
				fout << "DA\n";
			else
				fout << "NU\n";
	}
	
}