Cod sursa(job #3294350)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 21 aprilie 2025 21:10:54
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
//https://infoarena.ro/problema/disjoint
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("fast-math")
//#pragma GCC optimize ("unroll-loops")
//#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <vector>
//#include <cstring>
//#include <cmath>
//#include <bitset>
//#include <queue>
//#include <utility>
//#include <algorithm>
//#include <string>
//#include <map>
//#include <climits>
//#include <iomanip>
using namespace std;

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

int a[100005];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n, m, i, c, x, y;

	fin >> n >> m;
	for(i = 1; i <= m; ++i)
	{
		fin >> c >> x >> y;

		if (c == 1)
		{
			int xx = x, yy = y;

			while (a[xx])
			{
				xx = a[xx];
			}

			while (a[yy])
			{
				yy = a[yy];
			}

			a[xx] = yy;
		}
		else
		{
			int xx = x, yy = y;

			while (a[xx])
			{
				xx = a[xx];
			}

			while (a[yy])
			{
				yy = a[yy];
			}

			if (xx == yy)
				fout << "DA\n";
			else
				fout << "NU\n";
		}
	}


	return 0;
}