Cod sursa(job #2499480)

Utilizator tudor.bujdeiTudor Bujdei-Leonte tudor.bujdei Data 26 noiembrie 2019 10:31:48
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
// FFF.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>

#define NMAX 50001

using namespace std;

int n, q;
vector<int> v;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int main()
{
	fin >> n >> q;
	for (int i = 0; i < n; i++)v.push_back(i);
	for (int i = 0; i < q; i++)
	{
		int c;
		fin >> c;
		int x, y;
		fin >> x >> y;
		x--; y--;
		switch (c)
		{
		case 1:
			if (v[x] > v[y])
			{
				int aux = x;
				x = y;
				y = aux;
			}
			for (int j = 0; j < n; j++) if (j != x && j != y && v[j] == v[y]) v[j] = v[x];
			v[y] = v[x];
			break;
		case 2:
			if (v[x] == v[y]) fout << "DA\n";
			else fout << "NU\n";
			break;
		}
	}

	return 0;
}