Pagini recente » Borderou de evaluare (job #350219) | Diferente pentru problema/scmax2 intre reviziile 7 si 6 | Borderou de evaluare (job #2221866) | Diferente pentru problema/revolve intre reviziile 37 si 36 | Cod sursa (job #494366)
Cod sursa(job #494366)
#include <fstream>
using namespace std;
const char InFile[]="disjoint.in";
const char OutFile[]="disjoint.out";
const int MaxN=100050;
ifstream fin(InFile);
ofstream fout(OutFile);
int n,m,op,x,y,t[MaxN];
int root(int nod)
{
int fnod=nod;
while(t[nod]>0)
{
nod=t[nod];
}
while(t[fnod]>0)
{
t[fnod]=nod;
fnod=t[fnod];
}
return nod;
}
int main()
{
fin>>n>>m;
for(register int i=1;i<=n;++i)
{
t[i]=-1;
}
for(register int i=1;i<=m;++i)
{
fin>>op>>x>>y;
if(op==1)
{
int ra=root(x);
int rb=root(y);
if(t[ra]>t[rb])
{
swap(ra,rb);
}
t[ra]+=t[rb];
t[rb]=ra;
}
else
{
if(root(x)==root(y))
{
fout<<"DA\n";
}
else
{
fout<<"NU\n";
}
}
}
fin.close();
fout.close();
return 0;
}