Pagini recente » Cod sursa (job #422973) | Cod sursa (job #2858611) | Cod sursa (job #2454716) | Cod sursa (job #1527521) | Cod sursa (job #1790538)
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
ifstream fi("disjoint.in");
ofstream fo("disjoint.out");
int FIND(int x);
void UNION(int x, int y);
void init();
struct node {
int data;
node* father;
};
node* sets[100001];
int N, Q;
int main()
{
init();
for (int i = 1;i <= Q;i++)
{
int c, x, y;
fi >> c >> x >> y;
if (c == 2)
{
if (FIND(x) == FIND(y))
fo << "DA\n";
else fo << "NU\n";
}
else
UNION(x, y);
}
}
void init()
{
fi >> N >> Q;
for (int i = 1;i <= N;i++)
{
node* n = new node;
n->data = i;
n->father = NULL;
sets[i] = n;
}
}
void UNION(int x, int y)
{
node* n = sets[y];
while (n->father != NULL)
n = n->father;
sets[n->data]->father = sets[x];
return;
}
int FIND(int x)
{
node* n = sets[x];
while (n->father != NULL)
n = n->father;
return n->data;
}