Cod sursa(job #2073124)

Utilizator pelokbence575Pelok Benedek pelokbence575 Data 22 noiembrie 2017 18:44:20
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main() {
	ifstream input("disjoint.in");
	ofstream output("disjoint.out");

	int n, m;

	input >> n >> m;

	int* points = new int[n];

	for (int i = 0; i < n; ++i) {
		points[i] = i + 1;
	}

	int order, from, to;

	for (int i = 0; i < m; ++i) {
		input >> order >> from >> to;
		if (order == 1) {
			if (points[from - 1] != points[to - 1]) {
				if (!points[from - 1]) {
					points[from - 1] = points[to - 1];
				}
				else {
					if (!points[to - 1]) {
						points[to - 1] = points[from - 1];
					}
					else {
						int temp = points[to - 1];
						for (int j = 0; j < n; ++j) {
							if (points[j] == temp) {
								points[j] = points[from - 1];
							}
						}
					}
				}
			}
		}
		else {
			if (points[from - 1] == points[to - 1]) {
				output << "DA" << endl;
			}
			else {
				output << "NU" << endl;
			}
		}
	}
	delete[] points;
	input.close();
	output.close();
}