Cod sursa(job #3375072)

Utilizator lefterache_stefanLefterache Stefan lefterache_stefan Data 10 noiembrie 2026 10:26:21
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NODURI_MAXIM = 100005;
const int MUCHII_MAXIM = 100005;
int n, m;
int parinte[NODURI_MAXIM];
pair<int, int> muchii[MUCHII_MAXIM];

static inline int root(int x) {
	int rad = x;
	while (parinte[rad] != 0) {
		rad = parinte[rad];
	}
	int aux;
	while (x != rad) {
		aux = parinte[x];
		parinte[x] = rad;
		x = aux;
	}
	return rad;
}

int main() {
	fin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int operatie, a, b;
		fin >> operatie >> a >> b;
		const int x = root(a);
		const int y = root(b);
		const bool aceeasi_multime = x == y;
		if (operatie == 1) {
			if (aceeasi_multime) {
				continue;
			}
			parinte[y] = x;
		} else {
			fout << (aceeasi_multime ? "DA\n" : "NU\n");
		}
	}
}