Pagini recente » Cod sursa (job #303033) | Cod sursa (job #766353) | Cod sursa (job #149554) | Cod sursa (job #707539) | Cod sursa (job #1472539)
#include<iostream>
#include<fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
#define MAX 100001
struct Node
{
int parent;
int height;
}A[MAX];
int N, M;
int Find(int e)
{
if (A[e].parent)
{
A[e].parent = Find(A[e].parent);
return A[e].parent;
}
else
return e;
}
void Add(int x, int y)
{
x = Find(x);
y = Find(y);
if (A[x].height > A[y].height)
A[x].parent = y;
else if (A[x].height < A[y].height)
A[y].parent = x;
else
{
A[x].parent = y;
A[x].height += 1;
}
}
void operation(int e,int x,int y)
{
switch (e)
{
case 1:
Add(x, y);
break;
case 2:
int e1, e2;
e1 = Find(x);
e2 = Find(y);
if (e1 == e2)
out << "DA\n";
else
out << "NU\n";
break;
}
}
int main()
{
in >> N >> M;
int op, x, y;
for (int i = 1;i <= M;++i)
{
in >> op >> x >> y;
operation(op, x, y);
}
in.close();
out.close();
return 0;
}