Cod sursa(job #3233871)

Utilizator drsbosDarius Scripcaru drsbos Data 5 iunie 2024 11:27:58
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <string>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define oo 2000000
#define MOD 1000000007
using namespace std;

ifstream fin("a.in");
ofstream fout("a.out");




int t[100000];
int rang[100000];

int n, m,op,x,y;
int find(int k)
{
	if (t[k] == 0)
		return k;
	else
	{
		int x = find(t[k]);
		t[k] = x;
		return x;
	}
}

void Union(int x,int  y)
{
	if (rang[x] > rang[y])
	{
		t[y] = x;
	}
	else
	{
		t[x] = y;
		if (rang[x] == rang[y])
			rang[y]++;
	}
}
int main()
{
	fin >> n >> m;
	while (m--)
	{
		fin >> op >> x >> y;
		x = find(x);
		y = find(y);
		if (op == 2)
		{
			if (x == y)
			{
				fout << "DA" << endl;
			}
			else
			{
				fout << "NU" << endl;
			}
		}
		else
		{
			if (x != y)
				Union(x, y);
		}
	}
}