Pagini recente » Cod sursa (job #2240746) | Cod sursa (job #646974) | Cod sursa (job #718345) | Cod sursa (job #2663556) | Cod sursa (job #1705089)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Tree
{
int parent;
int depth;
Tree(int p, int d) : parent(p), depth(d){
}
};
ifstream in("disjoint.in");
ofstream out("disjoint.out");
vector<Tree> forest;
void swap(int x,int y)
{
int aux= x;
x = y;
y = aux;
}
int find(int x)
{
if (forest[x].parent == -1)
return x;
forest[x].parent = find(forest[x].parent);
return forest[x].parent;
}
void merge(int x, int y)
{
//set x to be greater
if (forest[x].depth < forest[y].depth)
swap(x,y);
if (forest[x].depth == forest[y].depth)
forest[x].depth++;
forest[y].parent = find(x);
}
int main()
{
int n, m;
in >> n >> m;
forest.resize(n+1, Tree(-1, 1));
for (int i = 0; i < m; i++)
{
int op, x, y;
in >> op >> x >> y;
if (op == 1)
merge(x,y);
if (op == 2)
if(find(x) == find(y))
cout <<"DA\n";
else
cout << "NU\n";
}
return 0;
}