Pagini recente » Cod sursa (job #703479) | Cod sursa (job #2043941) | Cod sursa (job #2868699) | Cod sursa (job #766124) | Cod sursa (job #2948841)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector <int> t,h;
int radacina(int x)
{
while(x!=t[x])
x=t[x];
return x;
}
void unifica(int x, int y)
{
//x si y radacinile arborilor care se ujnifica
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,c,x,y,rx,ry;
fin>>n>>m;
t.assign(n+1,0);
h.assign(n+1,1);
for(i=1;i<=n;i++)
t[i]=i;
for(i=1;i<=m;i++)
{
fin>>c>>x>>y;
rx=radacina(x);
ry=radacina(y);
if(c==1)
{
if(rx!=ry)
unifica(rx,ry);
}
else
if(rx==ry)
fout<<"DA\n";
else
fout<<"NU\n";
}
fin.close();
fout.close();
return 0;
}