Pagini recente » Cod sursa (job #734004) | Cod sursa (job #259617) | Cod sursa (job #1445431) | Cod sursa (job #1702853) | Cod sursa (job #2973583)
#include <fstream>
#define nmax 100005
using namespace std;
int n,m,c,x,y,root[nmax],len[nmax];
int cauta(int x)
{
if (root[x]==x)
return x;
return cauta(root[x]);
}
int merg (int a,int b)
{
if (len[a]<len[b])
swap(a,b);
len[a]+=len[b];
root[b]=a;
}
int main()
{
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
f>>n>>m;
for (int i=1;i<=n;i++)
root[i]=i,len[i]=1;
for (int i=1;i<=m;i++)
{
f>>c>>x>>y;
if (c==1)
{
int xs=cauta(x),ys=cauta(y);
if (xs!=ys)
merg(xs,ys);
}
else
{
int xs=cauta(x),ys=cauta(y);
if (xs==ys) g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
}