Pagini recente » Cod sursa (job #1972828) | Cod sursa (job #1420320) | Cod sursa (job #691620) | Cod sursa (job #1743892) | Cod sursa (job #2126584)
#include <fstream>
using namespace std;
ifstream is("disjoint.in");
ofstream os("disjoint.out");
int p[100001];
int n, m;
void Union(int x, int y);
int Find(int x);
int main()
{
int n, m;
is >> n >> m;
int op, x, y;
for ( int i = 1; i <= n; ++i )
p[i] = i;
for ( int i = 1; i <= m; ++i )
{
is >> op >> x >> y;
switch ( op )
{
case 1 :
Union(Find(x), Find(y));
break;
case 2 :
if ( Find(x) == Find(y) )
os << "DA" << '\n';
else
os << "NU" << '\n';
break;
}
}
is.close();
os.close();
return 0;
}
void Union(int x, int y)
{
p[x] = y;
}
int Find(int x)
{
if ( x != p[x] )
p[x] = Find(p[x]);
return p[x];
}