Pagini recente » Cod sursa (job #1593484) | Cod sursa (job #1646585) | Cod sursa (job #126672) | Cod sursa (job #1230421) | Cod sursa (job #2039049)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,m,p[100001];
int parinte(int nod)
{
if(p[nod]==nod)
return nod;
return p[nod]=parinte(p[nod]);
}
void unite(int x,int y)
{
p[parinte(x)]=parinte(y);
}
int main()
{
int cod,x,y;
in>>n>>m;
for(int i=1;i<=m;i++)
{
in>>cod>>x>>y;
if(!p[x])
p[x]=x;
if(!p[y])
p[y]=y;
if(cod==1)
unite(x,y);
else
{
int px,py;
px=parinte(x);
py=parinte(y);
if(p[px]==p[py])
out<<"DA"<<endl;
else
out<<"NU"<<endl;
}
}
in.close();
out.close();
return 0;
}