Pagini recente » Cod sursa (job #2315024) | Cod sursa (job #2218922) | Cod sursa (job #1638925) | Cod sursa (job #2746025) | Cod sursa (job #2419667)
#include <iostream>
//grad[x] reprez nr de noduri din comp
//find father
int tata[100000],grad[100000];
using namespace std;
int find_father(int nod)
{
if(tata[nod]==nod)
return nod;
tata[nod]=find_father(tata[nod]);
return tata[nod];
}
int main()
{
int n,m,i,x,y,cod;
cin>>n>>m;
for(i=1;i<=n;i++)
{
tata[i]=i;
grad[i]=1;
}
for(i=1;i<=m;i++)
{
cin>>cod>>x>>y;
if(cod==1)
{
int fx=find_father(x);
int fy=find_father(y);
if(grad[fx]<grad[fy])
{
tata[fx]=fy;
grad[fy]+=grad[fx];
}
else
{
tata[fy]=fx;
grad[fx]+=grad[fy];
}
}
else
if(cod==2)
{
int fx=find_father(x);
int fy=find_father(y);
if(fx==fy)
cout<<"DA"<<endl;
else
cout<<"NU"<<endl;
}
}
return 0;
}