Pagini recente » Cod sursa (job #931507) | Cod sursa (job #2630578) | Cod sursa (job #1019171) | Cod sursa (job #968287) | Cod sursa (job #2491555)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<int>t, h;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int varf(int x)
{
while(x!=t[x])
x = t[x];
return x;
}
void unire(int x, int y)
{
//x, y sunt radacinile arborilor
if(h[x] > h[y])
t[y] = x;
else
{
if(h[y] > h[x])
t[x] = y;
else
{
h[x]++;
t[y] = x;
}
}
}
int main()
{
int n, m, i, x, y, ty, tx, c;
fin>>n>>m;
for(i = 0; i<=n; i++)
{
t.push_back(i);
h.push_back(1);
}
/*for(i = 1; i<=m; i++)
{
cin>>x>>y;
tx = findset(x);
ty = findset(y);
if(tx!=ty)
{
unionset(tx, ty);//unific 2 arbori cu
}
}*/
for(i = 1; i<=m; i++)
{
fin>>c>>x>>y;
if(c == 1)
unire(x, y);
else
{
if(varf(x) == varf(y))
fout<<"DA"<<endl;
else
fout<<"NU"<<endl;
}
}
return 0;
}