Pagini recente » Cod sursa (job #2693245) | Cod sursa (job #1249331) | Cod sursa (job #2360067) | Cod sursa (job #1731119) | Cod sursa (job #2147991)
#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 )
{
if ( t[x] != t[y] )
UnionSet ( t[x] , t[y] ) ;
}
else
{
if ( FindSet(t[x]) == FindSet(t[y]))
fout << "DA" << endl ;
else
fout << "NU" << endl ;
}
}
return 0;
}