Pagini recente » Cod sursa (job #3165366) | Cod sursa (job #2416021) | Cod sursa (job #256201) | Cod sursa (job #895689) | Cod sursa (job #2736419)
#include <iostream>
#include <fstream>
using namespace std;
int n,m,i,t[100005],c,a,b,t1,t2,r[100005];
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata(int x)
{
int R, y;
//merg in sus pe arbore pana gasesc un nod care pointeaza catre el insusi
for (R = x; t[R] != R; R = t[R]);
//aplic compresia drumurilor
for (; t[x] != x;)
{
y = t[x];
t[x] = R;
x = y;
}
return R;
}
void unire(int t1, int t2)
{
if (r[t1]>r[t2])
t[t2]=t1;
else t[t1]=t[t2];
if (r[t1]==r[t2]) r[t2]++;
}
int main()
{
in>>n>>m;
for (i=1;i<=n;i++)
t[i]=i;
for (i=1;i<=m;i++)
{
in>>c;
if (c==1)
{
in>>a>>b;
t1=tata(a);
t2=tata(b);
unire(t1,t2);
}
else
{
in>>a>>b;
t1=tata(a);
t2=tata(b);
if (t1==t2) out<<"DA"<<endl;
else out<<"NU"<<endl;
}
}
}