Cod sursa(job #1801718)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 9 noiembrie 2016 16:12:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define x first
#define y second
#define mod 1000000007

using namespace std;
int n, pr[100100], m;
int x, a, b;

int find(int nod)
{
	if (pr[nod] == nod) return nod;
	int p = find(pr[nod]);
	pr[nod] = p;
	return p;
}

void unite(int a, int b)
{
	a = find(a);
	b = find(b);
	pr[a] = b;
}


int main()
{
	ifstream cin("disjoint.in");
	ofstream cout("disjoint.out");
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		pr[i] = i;
	for (int i = 1; i <= m; i++)
	{
		cin >> x >> a >> b;
		if (x == 1) unite(a, b);
			else if (find(a) == find(b)) cout << "DA\n";
					else cout << "NU\n";
	}
    return 0;
}