Pagini recente » Cod sursa (job #3312326) | Cod sursa (job #1040050) | Cod sursa (job #3308242) | Monitorul de evaluare | Cod sursa (job #2576790)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int nlim=100005;
int n,op,tt[nlim],rg[nlim];
void unire(int x, int y)
{
if(rg[x]<rg[y])
tt[x]=y;
if(rg[x]>rg[y])
tt[y]=x;
if(rg[x]==rg[y])
{
tt[x]=y;
rg[y]++;
}
}
int tatal(int x)
{
while(tt[x]!=0)
x=tt[x];
return x;
}
void citire()
{
f>>n>>op;
int z,x,y;
for(int i=1;i<=op;i++)
{
f>>z>>x>>y;
int tatal_x=tatal(x);
int tatal_y=tatal(y);
if(z==1)
unire(tatal_x,tatal_y);
if(z==2)
{
if(tatal_x==tatal_y)
g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
}
int main()
{
citire();
return 0;
}