Pagini recente » Cod sursa (job #2662766) | Cod sursa (job #227600) | Cod sursa (job #186936) | Cod sursa (job #1878612) | Cod sursa (job #2496264)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int sefx,sefy,tata[100001];
int sef(int x)
{
if(x==tata[x])return x;
else tata[x]=sef(tata[x]);
}
void unire(int x,int y)
{
sefx=sef(x);
sefy=sef(y);
tata[sef(x)]=sefy;
}
int main()
{
int n,m,cod,x,y,i;
in>>n>>m;
for(i=1;i<=n;i++)tata[i]=i;
for(i=1;i<=m;i++)
{
in>>cod>>x>>y;
if(cod==1)
{
unire(x,y);
}
else if(cod==2)
{
if(sef(x)==sef(y))out<<"DA"<<'\n';
else out<<"NU"<<'\n';
}
}
//for(i=1;i<=n;i++)out<<tata[i]<<" ";
return 0;
}