Cod sursa(job #2427355)

Utilizator teoschipor00Teodora Schipor teoschipor00 Data 31 mai 2019 17:03:39
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
#define Nmax 100003

using namespace std;

///---------FISIERE---------
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int tata[Nmax], grad[Nmax];

int findFather(int x)
{
	if (tata[x] == x)
		return x;
	else return findFather(tata[x]);
}

int main()
{
    ///--------CITIRE DATE--------
    int n, m, fx, fy, x, y, cod;
	f >> n >> m;

	for (int i = 1; i <= m; i++)
	{
		tata[i] = i;
		grad[i] = 1;
	}

	for (int i = 1; i <= m; i++)
	{
		f >> cod >> x >> y;
		fx = findFather(x);
		fy = findFather(y);

		if (cod == 1 && fx != fy)
			{
			    if (grad[fx] < grad[fy])
			{
				tata[fx] = fy;
				grad[fy] += grad[fx];
			}
			else
			{
				tata[fy] = fx;
				grad[fx] += grad[fy];
			}

			}
			else if(cod == 2)
            {
                if(findFather(x)==findFather(y))
                    g << "DA" << endl;
                else
                    g << "NU" << endl;
            }
	}

	return 0;
}