Pagini recente » Cod sursa (job #1351454) | Cod sursa (job #1347976) | Cod sursa (job #1175803) | Cod sursa (job #2843412) | Cod sursa (job #1377649)
#include <fstream>
#define nmax 100001
using namespace std;
ifstream is ("disjoint.in");
ofstream os ("disjoint.out");
int N, M, x, y, type;
int GR[nmax], NR[nmax];
void Read();
int Find(int);
void Union(int,int);
int main()
{
is >> N >> M;
for(int i = 1; i <= N; ++i)
GR[i] = i;
for(int i = 1; i <= M; ++i)
{
is >> type >> x >> y;
if(type == 1) Union(x, y);
else {
if(Find(x) == Find(y)) os << "DA\n";
else os << "NU\n";
}
}
is.close();
os.close();
return 0;
}
void Union(int x, int y)
{
if(NR[x] > NR[y])
GR[y] = x;
else
{
GR[x] = y;
++NR[y];
}
}
int Find(int x)
{
if(GR[x] == x)
return x;
return Find(GR[x]);
}