Pagini recente » Cod sursa (job #727020) | Cod sursa (job #2157276) | Cod sursa (job #1198855) | Cod sursa (job #451904) | Cod sursa (job #2605886)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int tata[100001], rang[100001], n, m, i, x, y, t;
int tata_multime( int nod )
{
if( tata[nod] != nod )
tata[nod] = tata_multime( tata[nod] );
return tata[nod];
}
void Union( int px, int py )
{
px = tata_multime( px );
py = tata_multime( py );
if( px != py )
{
if( rang[px] < rang[py] )
{
rang[py] += rang[px];
tata[px] = py;
}
else
{
rang[px] += rang[py];
tata[py] = px;
}
}
}
int main()
{
f >> n >> m;
for( i = 1; i <= n; ++ i )
{
tata[i] = i;
rang[i] = 1;
}
for( ; m; m -- )
{
f >> t >> x >> y;
if( t == 1 )
{
Union( x, y );
}
else
{
if( tata_multime(x) == tata_multime(y) )
g << "DA" << "\n";
else
g << "NU" << "\n";
}
}
return 0;
}