Pagini recente » Cod sursa (job #2898386) | Cod sursa (job #980921) | Cod sursa (job #673693) | Cod sursa (job #2894435) | Cod sursa (job #609554)
Cod sursa(job #609554)
#include<fstream>
using namespace std;
int n,m,tata[100010],h[100010];
int Find_compresie(int x)
{
int r=x;
while(tata[r])
r=tata[r];
int y=x,aux;
while(y!=r)
{
aux=tata[y];
tata[y]=r;
y=aux;
}
return r;
}
void Union_ponderate(int x,int y)
{
if(h[x]>h[y])
tata[y]=x;
else
{
tata[x]=y;
if(h[x]==h[y])
h[y]++;
}
}
int main()
{
int i,op,x,y,A,B;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>op>>x>>y;
if(op==1)
{
A=Find_compresie(x);
B=Find_compresie(y);
Union_ponderate(A,B);
}
else
{
A=Find_compresie(x);
B=Find_compresie(y);
if(A==B)
fout<<"DA\n";
else
fout<<"NU\n";
}
}
fin.close();
fout.close();
return 0;
}