Pagini recente » Cod sursa (job #2343909) | Cod sursa (job #2590290) | Cod sursa (job #752396) | Cod sursa (job #1106877) | Cod sursa (job #2658500)
#include <fstream>
#define Nmax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int Tata[Nmax], Dimensiune[Nmax];
int tata_multime( int Nod )
{
if( Nod != Tata[Nod] )
Tata[Nod] = tata_multime(Tata[Nod]);
return Tata[Nod];
}
void Unire( int x, int y )
{
x = tata_multime(x);
y = tata_multime(y);
if( Dimensiune[x] < Dimensiune[y] )
{
Dimensiune[y] += Dimensiune[x];
Tata[x] = y;
}
else
{
Dimensiune[x] += Dimensiune[y];
Tata[y] = x;
}
}
int main()
{
int vf, muchii;
fin >> vf >> muchii;
for( int i = 1; i <= vf; i ++ )
Tata[i] = i, Dimensiune[i] = 1;
for(int i = 1; i <= muchii; i ++)
{
int caz, a, b;
fin >> caz >> a >> b;
if( caz == 1 )
Unire( a, b );
else
{
if( tata_multime(a) != tata_multime(b) )
fout << "NU" << "\n";
else
fout << "DA" << "\n";
}
}
return 0;
}