Cod sursa(job #1801707)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 9 noiembrie 2016 16:02:23
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 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[100005], 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;
}

void scrie(int a, int b)
{
	(find(a) == find(b) ? cout << "DA\n" : cout << "NU\n"); 
}

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;
		(x == 1 ? unite(a, b) : (scrie(a, b)));
	}
    return 0;
}