Pagini recente » Cod sursa (job #3171751) | Cod sursa (job #3248124) | Cod sursa (job #616798) | Cod sursa (job #1972128) | Cod sursa (job #2238292)
#include <fstream>
using namespace std;
const int nmax=100000;
int t[nmax+1],h[nmax+1];
int findset (int x)
{
while (x!=t[x])
x=t[x];
return x;
}
void unionset (int x,int y)
{
if (h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else if (h[x]>h[y])
t[y]=x;
else t[x]=y;
}
ifstream fin ("disjoint.in");
ofstream fout("disjoint.out");
int main()
{
int n,m,i,x,y,tx,ty,c;
fin>>n>>m;
for (i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for (i=1;i<=m;i++)
{
fin>>c>>x>>y;
tx=findset(x);
ty=findset(y);
if (c==2)
if (tx==ty)
fout<<"DA";
else fout<<"NU";
else
{
if (tx!=ty)
unionset(tx,ty);
}
}
return 0;
}