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