Cod sursa(job #1609301)

Utilizator ArkinyStoica Alex Arkiny Data 22 februarie 2016 18:40:27
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include<fstream>
#include<vector>
using namespace std;

ifstream in("disjoint.in");
ofstream out("disjoint.out");

vector<int> v;
int *h[100010];

void add(int x, int y)
{
	if (h[x] == 0)
	{
		v.push_back(x);
		h[x] = &v[v.size() - 1];
		if (h[y] == 0)
			h[y] = h[x];
		else
			*h[y] = *h[x];
	}
	else
	{
		if (h[y] == 0)
			h[y] = h[x];
		else
			*h[y] = *h[x];
	}

}
bool check(int x, int y)
{
	if (h[y] == 0 || h[x] == 0)
		return 0;
	else if (*h[y] != *h[x])
		return 0;
	return 1;

}


int main()
{
	int N,M;

	in >> N >> M;
	for (int i = 1; i <= M; ++i)
	{
		int op, x, y;
		in >> op >> x >> y;
		if (op == 1)
			add(x, y);
		else
		{
			if (check(x, y) == 0)
				out << "NU\n";
			else
				out << "DA\n";
		}
	}

	return 0;
}