Pagini recente » Cod sursa (job #2283074) | Cod sursa (job #2865267) | Cod sursa (job #987703) | Cod sursa (job #1789828) | Cod sursa (job #2147964)
#include <fstream>
#include <vector>
using namespace std;
const int NMAX = 100000 ;
int t[NMAX+5] , h[NMAX+5] ;
ifstream fin("disjoint.in") ;
ofstream fout("disjoint.out") ;
int FindSet( int x )
{
while ( x != t[x] )
x = t[x] ;
return x ;
}
void UnionSet ( int x , int y )
{
if ( h[x] == h[y] )
{
t[y] = x ;
h[x] ++ ;
}
else
if ( h[x] > h[y] )
t[y] = x ;
else
t[x] = y ;
}
int main()
{ int n , m , i , j , x , y ;
fin >> n >> m ;
for ( i = 1 ; i <= n ; i ++ )
{
t[i] = i ;
h[i] = 1 ;
}
for ( i = 1 ; i <= m ; i ++ )
{
fin >> j >> x >> y ;
if ( j == 1 )
{
UnionSet ( x , y ) ;
}
else
{
if ( t[x] == FindSet (y))
fout << "DA" << endl ;
else
fout << "NU" << endl ;
}
}
return 0;
}