Pagini recente » Cod sursa (job #44469) | Cod sursa (job #449914) | Profil Pirojokwotly | Cod sursa (job #2161374) | Cod sursa (job #1852316)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
using VI = vector<int>;
VI p, l;
void Union( int x, int y );
int Find( int x );
int main()
{
int i, N, M, t, x, y;
fin >> N >> M;
p = l = VI(N + 1);
for ( i = 1; i <= N; ++i )
p[i] = i, l[i] = 1;
for ( i = 1; i <= M; ++i )
{
fin >> t >> x >> y;
if ( t == 1 )
{
Union(x, y);
}
else
if ( Find(x) == Find(y) )
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
fin.close();
fout.close();
return 0;
}
void Union( int x, int y )
{
if ( l[x] < l[y] )
p[x] = y;
else
{
p[y] = x;
if ( l[x] == l[y] )
l[x]++;
}
}
int Find( int x )
{
if ( x == p[x] ) return x;
return p[x] = Find(p[x]);
}