Pagini recente » Cod sursa (job #154805) | Cod sursa (job #1831941) | Cod sursa (job #750787) | Cod sursa (job #1658591) | Cod sursa (job #1542467)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int root[100005], size[100005], n, m, x, y, tx, ty, i, cod;
int find(int a)
{
int t, i;
for(t=a;t!=root[t];t=root[t]);
i=a;
while(i!=t)
{
x=root[i];
root[i]=t;
i=x;
size[t]++;
}
return t;
}
void unite(int a, int b)
{
int ta, tb;
ta=find(a);
tb=find(b);
if(size[ta]<size[tb])
{
root[tb]=ta;
size[ta]++;
}
if(size[ta]>=size[tb])
{
root[ta]=tb;
size[tb]++;
}
}
int main()
{
fin>>n>>m;
for(i=1;i<=n;i++)
root[i]=i;
for(i=1;i<=m;i++)
{
fin>>cod;
if(cod==1)
{
fin>>x>>y;
unite(x,y);
}
else
{
fin>>x>>y;
tx=find(x);
ty=find(y);
if(tx==ty)
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}