Cod sursa(job #2567253)

Utilizator Iulia25Hosu Iulia Iulia25 Data 3 martie 2020 16:12:57
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>

using namespace std;

ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
const int n=100005;
int N,M,tip,x,y;
int tata[n];
int multime(int g)
{
 int copieg=g;
 while(tata[g]>0)
 {
  g=tata[g];
 }
 while(tata[copieg]!=g && tata[copieg] > 0)
 {
 int aux=tata[copieg];
 tata[copieg]=g;
 copieg=aux;
 }
 return g;
}
int main()
{
    cin>>N>>M;
	  for(int i=1;i<=N;++i)
	  tata[i]=-1;
    for(int i=1;i<=M;++i)
    {
     cin>>tip>>x>>y;
     if(tip==2)
     {
			if (multime(x) == multime(y))
				cout << "DA\n";
			else cout << "NU\n";
     }
     if (tip == 1)	{
			int tatasupremx=multime(x);
			int tatasupremy=multime(y);
			if (tatasupremx != tatasupremy)	 {
				if (tata[tatasupremx] < tata[tatasupremy])	{
					tata[tatasupremx] += tata[tatasupremy];
					tata[tatasupremy] = tatasupremx;
				}
				else	{
					tata[tatasupremy] += tata[tatasupremx];
					tata[tatasupremx] = tatasupremy;
				}
			}
     }
    }
    return 0;
}