Pagini recente » Cod sursa (job #1791592) | Cod sursa (job #2144614) | Cod sursa (job #2759993) | Cod sursa (job #593279) | Cod sursa (job #2941837)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100005];
int tata(int nod)
{
if(t[nod]==nod)
return nod;
t[nod]=tata(t[nod]);
return t[nod];
}
int join(int x, int y)
{
x=tata(x);
y=tata(y);
t[x]=y;
}
int main()
{
int n,m;
in>>n>>m;
for(int i=1;i<=n;i++)
t[i]=i;
int cod,x,y;
for(int i=0;i<m;i++)
{
in>>cod>>x>>y;
if(cod==1)
join(x,y);
else
{
if(tata(x)==tata(y))
out<<"DA\n";
else
out<<"NU\n";
}
}
}