Pagini recente » Cod sursa (job #792521) | Cod sursa (job #1709985) | Cod sursa (job #2778638) | Monitorul de evaluare | Cod sursa (job #832736)
Cod sursa(job #832736)
#include <fstream>
#include <cstring>
using namespace std;
const char iname[] = "disjoint.in";
const char oname[] = "disjoint.out";
ifstream fin(iname);
ofstream fout(oname);
int comp[ 100010 ];
inline int cauta ( int x )
{
int r , y;
r = x;
while ( comp[ r ] > 0 )
r = comp[ r ];
while ( comp[ x ] > 0 )
{
y = comp[ x ];
comp[ x ] = r;
x = y;
}
return r;
}
inline void uneste ( int x , int y )
{
if ( comp[ x ] > comp[ y ] )
{
comp[ y ] += comp[ x ];
comp[ x ] = y;
}
else
{
comp[ x ] += comp[ y ];
comp[ y ] = x;
}
}
int main()
{
int n , m , x , y , cod , i;
fin >> n >> m;
memset ( comp , -1 , sizeof( comp ) );
for ( i = 1; i <= m; ++i )
{
fin >> cod >> x >> y;
if ( cod == 2 )
{
if ( cauta( x ) == cauta( y ) )
fout << "DA\n";
else
fout << "NU\n";
}
else
uneste ( cauta( x ) , cauta( y ) );
}
return 0;
}