Pagini recente » Cod sursa (job #1590338) | Cod sursa (job #2358661) | Cod sursa (job #1972188) | Cod sursa (job #2508921) | Cod sursa (job #2692861)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[100001],height[100001];
void compresie(int nod, int r)
{
///compresia drumurilor
int next = nod;
while(next != r )
{
int previous = next;
next = tata[next];
tata[previous] = r;
}
}
int check(int a)
{
int initial = a;
while(tata[a])
{
a = tata[a];
}
compresie(initial,a);
return a;
}
bool unite(int a, int b)
{
///a si b vor reprezenta acum radaciniile multimiilor
if(height[a] > height[b])
{
tata[b] = a;
}
else if(height[a] < height[b])
{
tata[a] = b;
}
else
{
tata[a] = b;
height[b] ++;
}
}
int main()
{
int n,m;
fin >> n >> m;
for(int i = 1; i <= n; i++)
height[i] = 1;
for(int i = 1; i <=m ; i++)
{
int tip,a,b;
fin >> tip >> a >> b;
if(tip == 1)
{
unite(check(a),check(b));
}
else
{
if(check(b) == check(a))
fout<<"DA\n";
else
fout<<"NU\n";
}
}
return 0;
}