Pagini recente » Cod sursa (job #3310689) | Cod sursa (job #942839) | Cod sursa (job #2933791) | Cod sursa (job #2857762) | Cod sursa (job #2553514)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int nlim=100005;
int n,m,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]!=x)
x=tt[x];
return x;
}
void citire()
{
int cod,x,y;
f>>n>>m;
for(int i=1;i<=n;i++)
{
tt[i]=i;
rg[i]=1;
}
for(int i=1;i<=m;i++)
{
f>>cod>>x>>y;
if(cod==1) unire(x,y);
if(cod==2)
{
if(tatal(x)==tatal(y)) g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
}
int main()
{
citire();
return 0;
}