Cod sursa(job #3285317)

Utilizator crina2120Arnautu Cristina-Crina crina2120 Data 12 martie 2025 18:21:54
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
using namespace std;

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

int t[100003], n, m;

void Union(int x, int y)
{
    t[x] = y;
}

int Find(int x)
{
    if (t[x] == 0) return x;
    return t[x] = Find(t[x]);
}

int main()
{
    int i, op, x, y;
	fin >> n >> m;
	for (i = 1; i <= m; i++)
	{
		fin >> op >> x >> y;
		x = Find(x);
		y = Find(y);
		if (op == 1 && x != y) Union(x, y);
		else if (op == 2)
		{
			if (x != y) fout << "NU\n";
			else fout << "DA\n";
		}
	}
    return 0;
}
//0, 1, 3-5, 7-18, 21, 22, 24-27, 29, 30, 34-36, 42, 44, 45, 47, 48, 51, 54, 56-58