Pagini recente » Cod sursa (job #2157479) | Cod sursa (job #1207575) | Cod sursa (job #18416) | Cod sursa (job #2300384) | Cod sursa (job #2797227)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX=100001;
int t[NMAX], h[NMAX];
int findSet (int x)
{
while (t[x]!=x)
x=t[x];
return x;
}
void UnionSet (int x, int y)
{
// x si y sunt radacini
if (h[x]>h[y])
t[y]=x;
else
if (h[y]>h[x])
t[x]=y;
else
{
t[y]=x;
h[x]++;
}
}
int main()
{
int n, i, m, x, y, tx, ty, op;
fin>>n>>m;
for (i=1; i<=n; i++)
{
t[i]=i;
h[i]=1;
}
for (i=1; i<=m; i++)
{
fin>>op>>x>>y;
if (op==1)
{
tx=findSet(x);
ty=findSet(y);
if (tx!=ty)
UnionSet(tx, ty);
}
else
{
tx=findSet(x);
ty=findSet(y);
if (tx==ty)
fout<<"DA\n";
else
fout<<"NU\n";
}
}
return 0;
}