Pagini recente » Cod sursa (job #528522) | Cod sursa (job #685842) | Cod sursa (job #848307) | Cod sursa (job #1788965) | Cod sursa (job #2410480)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,c,x,y;
int a[100005],t[100005];
int tata(int x)
{
int p=x;
while(t[x]!=x)
{
x=t[x];
}
while(t[p]!=p)
{
int p1=t[p];
t[p]=x;
p=p1;
}
return x;
}
void unific(int a,int b)
{
a=tata(a);
b=tata(b);
t[a]=t[b];
}
int main()
{
int i,j;
f>>n>>m;
for(i=1;i<=n;i++) {t[i]=i;a[i]=i;}
for(i=1;i<=m;i++)
{
f>>c>>x>>y;
if(c==1)
{
unific(x,y);
}
else
{
if(tata(x)==tata(y)) g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
return 0;
}