Pagini recente » Cod sursa (job #3162392) | Cod sursa (job #323879) | Cod sursa (job #1874289) | Cod sursa (job #2155688) | Cod sursa (job #2452969)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int NMAX=100000;
int t[NMAX+5], h[NMAX+5];
int FindSet(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void UnionSet(int x, int y)
{
/// x si y sunt radacinile celor 2 arbori
if(h[x]==h[y])
{
t[x]=y;
h[y]++;
}
else
{
if(h[x]<h[y])
t[y]=x;
else
t[x]=y;
}
}
int main()
{
int n, m, x, y, tx, ty, i, val;
fin>>n>>m;
for(i=1;i<=n;++i)
{
h[i]=1;
t[i]=i;
}
for(i=1;i<=m;++i)
{
fin>>val>>x>>y;
if(val==1)
{
tx=FindSet(x);
ty=FindSet(y);
UnionSet(tx, ty);
}
else
{
tx=FindSet(x);
ty=FindSet(y);
if(tx==ty)
fout<<"DA"<<"\n";
else
fout<<"NU"<<"\n";
}
}
return 0;
}